SpringBoot多文件上传,文件下载
【摘要】
SpringBoot多文件上传
一、文件上传
@PostMapping("/fileUpload")
public ResponseTemplate handleFormUploadFile(...
SpringBoot多文件上传
一、文件上传
@PostMapping("/fileUpload")
public ResponseTemplate handleFormUploadFile(@RequestParam("uploadfile") List<MultipartFile> uploadfile,
HttpServletRequest request) {
if (!uploadfile.isEmpty() && uploadfile.size() > 0) {
for (MultipartFile file : uploadfile) {
String dirPath = System.getProperty("user.dir")+"/src/main/resources/images/voice/";
String name = file.getOriginalFilename();
File filePath = new File(dirPath);
if (!filePath.exists()) {
filePath.mkdirs();
}
System.out.println(filePath);
try {
System.out.println(file.getSize());
file.transferTo(new File(dirPath + name));
} catch (Exception e) {
e.printStackTrace();
return ResFailTemplate.builder().message(e.toString()).build();
}
}
} else {
return ResFailTemplate.builder().message("not file").build();
}
return ResSuccessTemplate.builder().build();
}
- 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
二、上传限制
application.properties
#文件上传大小
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=300MB
- 1
- 2
- 3
三、文件下载
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
@RequestMapping("/download")
public ResponseEntity<byte[]> fileDownload(String filename, HttpServletRequest request) throws Exception {
System.out.println(filename);
String path = System.getProperty("user.dir")+"/src/main/resources/images/upload/";
// String path = request.getServletContext().getRealPath("/upload/");
System.out.println(path);
File file = new File(path+File.separator+filename);
// filename = FileType.getFilename(request, filename);
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", filename);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.OK);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
文章来源: blog.csdn.net,作者:小毕超,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43692950/article/details/107443354
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)