springboot + vue+elementUI實(shí)現(xiàn)圖片上傳功能
1.實(shí)現(xiàn)背景
前端上傳一張圖片,存到后端數(shù)據(jù)庫(kù),并將圖片回顯到頁(yè)面上。上傳組件使用現(xiàn)成的elementUI的el-upload。、
2.前端頁(yè)面
<el-upload class="upload-demo" action="http://xxxx.xxx.xxx:9090/file/upload" :show-file-list="false" multiple :limit="3" :on-success="handleAvatarSuccess1" > <img v-if="package1" :src="package1" class="avatar" alt=""> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload>
點(diǎn)擊上傳后,將圖片發(fā)送到action
后面的接口,之后后端返回圖片,回顯到img標(biāo)簽。
3.接口實(shí)現(xiàn)
前提:SQL已有一張image表:
application.yml文件中配置圖片存儲(chǔ)的位置
files: upload: path: /www/nndemo/sb/ #這里是服務(wù)器的文件位置,如果是本地項(xiàng)目,改成某磁盤某文件夾即可
接口實(shí)現(xiàn):
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 前端傳遞過(guò)來(lái)的文件 */ @RestController @RequestMapping("/file") public class FileController { @Autowired FileMapper fileMapper; @Value("${files.upload.path}") private String fileUploadPath; //圖片最終存儲(chǔ)的位置 @PostMapping("/upload") public String upload(@RequestParam MultipartFile file) throws IOException { String originalFilename = file.getOriginalFilename(); //原始名稱 String type = FileUtil.extName(originalFilename);//文件類型 long size = file.getSize(); //大小 //存儲(chǔ)到磁盤 File uploadParentFile = new File(fileUploadPath); if (!uploadParentFile.exists()){ //文件目錄是否存在 uploadParentFile.mkdirs(); } //定義一個(gè)文件唯一標(biāo)識(shí)碼 String uuid = IdUtil.fastSimpleUUID(); String fileUuid = uuid + StrUtil.DOT + type; File uploadFile = new File(fileUploadPath + fileUuid); //把獲取到的文件存儲(chǔ)到磁盤中去 file.transferTo(uploadFile); //存儲(chǔ)到數(shù)據(jù)庫(kù) 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); // 存入數(shù)據(jù)庫(kù),這里項(xiàng)目比較簡(jiǎn)單,沒有三層架構(gòu) 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實(shí)現(xiàn):
@Mapper public interface FileMapper { @Insert("insert into images(name,size,type,url) values (#{name},#{size},#{type},#{url})") void saveFile(Images files); }
到此這篇關(guān)于springboot + vue+elementUI實(shí)現(xiàn)圖片上傳功能的文章就介紹到這了,更多相關(guān)springboot vue圖片上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Springboot+Vue實(shí)現(xiàn)文件上傳和下載功能
- 基于SpringBoot和Vue實(shí)現(xiàn)頭像上傳與回顯功能
- Vue?+?SpringBoot?實(shí)現(xiàn)文件的斷點(diǎn)上傳、秒傳存儲(chǔ)到Minio的操作方法
- springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼
- Java實(shí)現(xiàn)大文件的分片上傳與下載(springboot+vue3)
- springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能
- 利用Springboot+vue實(shí)現(xiàn)圖片上傳至數(shù)據(jù)庫(kù)并顯示的全過(guò)程
- Vue+Element+Springboot圖片上傳的實(shí)現(xiàn)示例
- Springboot+Vue-Cropper實(shí)現(xiàn)頭像剪切上傳效果
相關(guān)文章
vue基礎(chǔ)之使用get、post、jsonp實(shí)現(xiàn)交互功能示例
這篇文章主要介紹了vue基礎(chǔ)之使用get、post、jsonp實(shí)現(xiàn)交互功能,結(jié)合實(shí)例形式分析了vue.js中g(shù)et、post及jsonp針對(duì)本地文件、網(wǎng)絡(luò)接口實(shí)現(xiàn)交互功能相關(guān)操作技巧,需要的朋友可以參考下2019-03-03解決VUE項(xiàng)目localhost端口服務(wù)器拒絕連接,只能用127.0.0.1的問(wèn)題
這篇文章主要介紹了解決VUE項(xiàng)目localhost端口服務(wù)器拒絕連接,只能用127.0.0.1的問(wèn)題2020-08-08Vue.js實(shí)現(xiàn)的表格增加刪除demo示例
這篇文章主要介紹了Vue.js實(shí)現(xiàn)的表格增加刪除demo,結(jié)合實(shí)例形式分析了vue.js數(shù)據(jù)綁定及元素增加、刪除等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05實(shí)例分析vue循環(huán)列表動(dòng)態(tài)數(shù)據(jù)的處理方法
本篇文章給大家詳細(xì)分享了關(guān)于vue循環(huán)列表動(dòng)態(tài)數(shù)據(jù)的處理方法以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。2018-09-09