文件上传:
Java中文件上传一共涉及到两个组件,CommonsMultipartResolver和StandardServletMultipartResolver,其中CommonsMultipartResolver使用commons-fileupload来处理multipart请求,StandardServletMultipartResolver基于Servlet3.0来处理multipart请求的。如果使用StandardServletMultipartResolver则不需要额外的jar包。Tomcat7.0开始支持Servlet3.0,SpringBoot2.0.4内置Tomcat是8.5.32,所以可以直接使用StandardServletMultipartResolver。
SpringBoot提供的文件上传自动化配置类MultipartAutoConfiguration中,
@ConditionalOnMissingBean,如果没有提供MultipartResolver,那么默认StandardServletMultipartResolver
@ConditionalOnMissingBean({MultipartResolver.class})
public StandardServletMultipartResolver multipartResolver() {
StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();
multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily());
return multipartResolver;
}
1.单文件上传:
创建upload.html文件
controller:
@PostMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req) {
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
System.out.println(realPath);
String format = sdf.format(new Date());
File folder = new File(realPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
uploadFile.transferTo(new File(folder, newName));
String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
return filePath;
} catch (IOException e) {
// e.printStackTrace();
}
return "上传失败!";
}
在application.properties中配置对文件上传的更多细节
#开启文件上传支持
spring.servlet.multipart.enabled=true
#文件写入磁盘阈值
spring.servlet.multipart.file-size-threshold=0
#上传文件临时保存位置
spring.servlet.multipart.location=E:\\temp
#上传单个文件最大大小1MB
spring.servlet.multipart.max-file-size=1MB
#多文件上传时总大小100MB
spring.servlet.multipart.max-request-size=100MB
#文件是否延迟解析false
spring.servlet.multipart.resolve-lazily=false
2.多文件上传:
upload.html
<form action="/uploads" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFiles" value="请选择文件" multiple>
<input type="submit" value="上传">
</form>
controller:
@PostMapping("/uploads")
public String upload(MultipartFile[] uploadFiles, HttpServletRequest req) {
for (MultipartFile uploadFile : uploadFiles) {
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
System.out.println(realPath);
String format = sdf.format(new Date());
File folder = new File(realPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
uploadFile.transferTo(new File(folder, newName));
String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
return filePath;
System.out.println(filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
return "上传失败!";
}