文件上传
controller层
- 使用
MultipartFile
类型接收单文件,多文件使用MultipartFile[]
- 一般直接使用
@RequestParam("file") MultipartFile file
来接收 - 也可以在入参使用
MultipartHttpServletRequest
,后续使用getFiles(参数名)
或getFileMap()
方法获取到文件集合,再取出集合里的MultipartFile
(单个文件只取第一个)
注意:
- 必须使用 Post,且只能是 multipart/form-data 的方式,这是由传输机制导致的。不然服务器得不到上传的文件。GET方法是附加在URL后面的,而文件是二进制编码不能附加到 URL 后面 (上传的文件实际都是放在请求体里面的)
- 使用
@RequestParam
来限定参数名,@RequestParam
入参支持 Post 和 Get,是用来处理ContentType
为 application/x-www-form-urlencoded(不指定时默认的) 和 multipart/form-data 等格式的参数的。只有 application/json、application/xml 等格式的数据,才使用@RequestBody
来处理。 - 可以看到
@RequestParam
实际是与ContentType
挂钩的,而大多数时候如果接收是对象,忘记加注解,也可以接收到 application/x-www-form-urlencoded 的参数,这是因为 spring 默认对应了@ModelAttribute
注解,等同于@RequestParam
只不过参数是一个实体对象(两者也就是"MyOrder.orderAddress"和"orderAddress"的区别)。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23// 方式1 @PostMapping("/upload") public Result<?> multiImport(@RequestParam("uploadFile") MultipartFile[] uploadFile) { System.out.println(uploadFile.length); for (MultipartFile multipartFile:uploadFile) { // 文件名 multipartFile.getOriginalFilename()); // 获取文件 multipartFile.getInputStream(); } return Result.ok(); } // 方式2 @PostMapping("/upload") public Result<?> importInfo(MultipartHttpServletRequest multipartRequest) { Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { // 获取上传文件对象 MultipartFile file = entity.getValue(); // 获取文件 file.getInputStream(); ...
Vue 前端
复制代码
1
2
3
4
5
6
7
8
9
10
11
12<a href="https://file.download.com/huge.xlsx"> <a-button type="primary" icon="download" style="margin-left: 8px">导出模板</a-button> </a> <a-upload style="margin-left: 8px" @change="handleInfoImport" :showUploadList="false" :action="importInfo" :headers="header"> <a-button type="primary" icon="upload">上传信息</a-button> </a-upload>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15data() { return { header: { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) }, url: { importInfo: '/baseinfo/importInfo' } }} importInfo() { // 返回请求Url:服务器地址 + 请求API return `${window._CONFIG['domianURL']}${this.url.importInfo}` }, handleInfoImport(info) { if (info.file.response.success) { this.$message.info('信息导入成功,共导入' + info.file.response.result.total + '条') } else { this.$message.error(info.file.response.message) } }
普通Form表单提交
- 缺点:页面会刷新
复制代码
1
2
3
4
5
6<form id="form111" enctype="multipart/form-data" method="post" action="#"> <!-- 可以选择多个文件,且只能选图片 --> <input type="file" name="files" multiple id="test"> <input type="button" id="btn"> </form>
文件下载
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42@RestController @RequestMapping("/file") public class FileResource { @GetMapping("{name:.+}") public void download(HttpServletResponse response, @PathVariable("name") String name) { try { Path path = Paths.get(name); InputStream input; if (!Files.exists(path)) { // 拿到 resources 下文件 ClassPathXmlApplicationContext ClassPathResource resource = new ClassPathResource(name); if (!resource.exists()) throw new RuntimeException("文件不存在!"); input = resource.getInputStream(); } else { input = Files.newInputStream(path); response.setContentLength((int) Files.size(path)); } //region response constructor String mimeType = URLConnection.guessContentTypeFromName(name); if (mimeType == null) // unknown mimetype so set the mimetype to application/octet-stream mimeType = "application/octet-stream"; response.setContentType(mimeType); /* In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally. Here we have mentioned it to show attachment */ response.setHeader( "Content-Disposition", "attachment; filename=" + URLEncoder.encode(name, "UTF-8")); //endregion StreamUtils.copy(input, response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("下载失败!" + e.getMessage()); } } }
Content-Disposition
- attachment: 用附件的形式下载
- inline: 用浏览器打开 (前提: 给出了文件类型, 并且浏览器能打开该文件类型)
最后
以上就是霸气帽子最近收集整理的关于Springboot 文件上传、文件下载文件上传文件下载的全部内容,更多相关Springboot内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复