SpringBoot3 +vue3 实现文件上传和下载
文件下载
新建一个fileController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@GetMapping("/download/{fileName}") public void download(@PathVariable String fileName, HttpServletResponse response) throws Exception { String filePath = System.getProperty("user.dir")+ "/files/"; String realPath = filePath + fileName; boolean exist = FileUtil.exist(realPath); if(!exist) { throw new CustomerException("文件不存在"); } byte[] bytes = FileUtil.readBytes(realPath); ServletOutputStream os = response.getOutputStream(); os.write(bytes); os.flush(); os.close(); }
|
以附件的形式下载(可忽略)
1 2
| response.addHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName)); response.setContentType("application/octet-stream");
|
接口放行,不鉴权
1 2 3 4 5
| @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(jwtInterceptor).addPathPatterns("/**") .excludePathPatterns("/login","/register","/files/download/**"); }
|
访问成功!

纯文本,pdf,也是可以的
浏览器根据文件类型来判断能不能打开
文件上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@PostMapping("/upload") public Result upload(@RequestParam("file")MultipartFile file) throws Exception { String filePath = System.getProperty("user.dir")+ "/files/"; if(FileUtil.isDirectory(filePath)){ FileUtil.mkdir(filePath); } byte[] bytes = file.getBytes(); String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename(); FileUtil.writeBytes(bytes,filePath+fileName); String url = "http://localhost:9999/files/download/" +fileName; return Result.success(url); }
|
在数据库中的user表哪里新增字段avatar
前端对接
文件上传到数据库存储额是一串url链接的字符串
表单上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <el-form-item prop="avatar" label="头像" > <el-upload action="http://localhost:9999/files/upload" :headers="{ token: data.user.token }" :on-success="handleFileSuccess" list-type="picture" > <el-button type="primary">上传头像</el-button> </el-upload> </el-form-item>
const handleFileSuccess = (res) => { data.form.avatar = res.data }
|
表格显示图片
1 2 3 4 5 6 7
| <el-table-column label="头像"> <template #default="scope"> <el-image v-if="scope.row.avatar" :preview-src-list="[scope.row.avatar]" :src="scope.row.avatar" style="width: 40px; height: 40px;border-radius: 50%;display: block;" /> </template> </el-table-column>
|
图片预览效果不理想

加上 :preview-teleported="true" 即可解决~
1 2 3 4 5 6 7
| <el-table-column label="头像"> <template #default="scope"> <el-image v-if="scope.row.avatar" :preview-src-list="[scope.row.avatar]" :src="scope.row.avatar" :preview-teleported="true" style="width: 40px; height: 40px;border-radius: 50%;display: block;" /> </template> </el-table-column>
|
SpringBoot3 +vue3 实现文件上传和下载