网站与网页设计,兰州装修公司官网,网站备案要什么,好三网网站1.实现背景
前端上传一张图片#xff0c;存到后端数据库#xff0c;并将图片回显到页面上。上传组件使用现成的elementUI的el-upload。、
2.前端页面 el-uploadclassupload-demoactionhttp://xxxx.xxx.xxx:9090/file/upload :show-file-list存到后端数据库并将图片回显到页面上。上传组件使用现成的elementUI的el-upload。、
2.前端页面 el-uploadclassupload-demoactionhttp://xxxx.xxx.xxx:9090/file/upload :show-file-listfalsemultiple:limit3:on-successhandleAvatarSuccess1img v-ifpackage1 :srcpackage1 classavatar alti v-else classel-icon-plus avatar-uploader-icon/i/el-upload点击上传后将图片发送到action后面的接口之后后端返回图片回显到img标签。
接口实现
前提SQL已有一张image表
application.yml文件中配置图片存储的位置
files:upload:path: /www/nndemo/sb/ #这里是服务器的文件位置如果是本地项目改成某磁盘某文件夹即可接口实现
package com.tt.springboot.controller;import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.tt.springboot.entity.Images;
import com.tt.springboot.mapper.FileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;/*** author TT* date 2023-10-26 14:47:13* description 文件上传下载接口* parms file 前端传递过来的文件*/RestController
RequestMapping(/file)
public class FileController {AutowiredFileMapper fileMapper;Value(${files.upload.path})private String fileUploadPath; //图片最终存储的位置PostMapping(/upload)public String upload(RequestParam MultipartFile file) throws IOException {String originalFilename file.getOriginalFilename(); //原始名称String type FileUtil.extName(originalFilename);//文件类型long size file.getSize(); //大小//存储到磁盘File uploadParentFile new File(fileUploadPath);if (!uploadParentFile.exists()){ //文件目录是否存在uploadParentFile.mkdirs();}//定义一个文件唯一标识码String uuid IdUtil.fastSimpleUUID();String fileUuid uuid StrUtil.DOT type;File uploadFile new File(fileUploadPath fileUuid);//把获取到的文件存储到磁盘中去file.transferTo(uploadFile);//存储到数据库String url http://xxxx.xxxx.xxx:9090/file/ fileUuid;Images saveFiles new Images();saveFiles.setName(originalFilename);saveFiles.setSize(size);saveFiles.setType(type);saveFiles.setUrl(url);fileMapper.saveFile(saveFiles); // 存入数据库这里项目比较简单没有三层架构return url;}GetMapping(/{fileUUID})public void download( HttpServletResponse response, PathVariable String fileUUID) throws IOException {File uploadFile new File(fileUploadPath fileUUID);ServletOutputStream outputStream response.getOutputStream();response.setHeader(Content-Disposition,attachment;filename URLEncoder.encode(fileUUID,UTF-8));response.setContentType(application/octet-stream);outputStream.write(FileUtil.readBytes(uploadFile));outputStream.flush();;outputStream.close();}
}fillMapper实现
Mapper
public interface FileMapper {Insert(insert into images(name,size,type,url) values (#{name},#{size},#{type},#{url}))void saveFile(Images files);
} 文章转载自: http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.qpqcq.cn.gov.cn.qpqcq.cn http://www.morning.htbsk.cn.gov.cn.htbsk.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.jlgjn.cn.gov.cn.jlgjn.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.pyswr.cn.gov.cn.pyswr.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.htrzp.cn.gov.cn.htrzp.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.sbncr.cn.gov.cn.sbncr.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.yrfxb.cn.gov.cn.yrfxb.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.rqhdt.cn.gov.cn.rqhdt.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.liyixun.com.gov.cn.liyixun.com http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.wkwds.cn.gov.cn.wkwds.cn http://www.morning.xwgbr.cn.gov.cn.xwgbr.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.osshjj.cn.gov.cn.osshjj.cn http://www.morning.yrms.cn.gov.cn.yrms.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.hrgxk.cn.gov.cn.hrgxk.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.sbdqy.cn.gov.cn.sbdqy.cn http://www.morning.zcckq.cn.gov.cn.zcckq.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.shsh1688.com.gov.cn.shsh1688.com http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn