1: Introduction to springMVC file upload
Among the 9 major components of springMVC , one of them is MultipartResolver
the file upload parsing component. When the component is a file upload request, the HttpServletRequest class will be parsed and generated as the MultipartHttpServletRequest class. This class inherits HttpServletRequest while also implementing The MultipartRequest
source code of the interface is as follows:
public interface MultipartRequest {
Iterator<String> getFileNames();
@Nullable
MultipartFile getFile(String name);
List<MultipartFile> getFiles(String name);
Map<String, MultipartFile> getFileMap();
MultiValueMap<String, MultipartFile> getMultiFileMap();
@Nullable
String getMultipartContentType(String paramOrFileName);
}
You can see that there are many file-related methods defined in the interface, so MultipartHttpServletRequest has it 操作文件的能力
. For the MultipartResolver interface, there are two implementation classes, one is CommonMultipartResolver
, the other is StandardServletMultipartResolver
, because the former needs to rely on the third-party package of apache, and the latter only needs the servlet API, so in actual use, StandardMultipartResolver is used more. Therefore, to complete the file upload operation, as long as it is able to create a StandardMultipartResolver, and in springboot, when the upload file parser is not specified, it is the StandardMultipartResolver that is created. The automatically created source code is as follows:
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({
Servlet.class, StandardServletMultipartResolver.class, MultipartConfigElement.class })
@ConditionalOnProperty(prefix = "spring.servlet.multipart", name = "enabled", matchIfMissing = true)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(MultipartProperties.class)
public class MultipartAutoConfiguration {
@Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
@ConditionalOnMissingBean(MultipartResolver.class)
public StandardServletMultipartResolver multipartResolver() {
StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();
multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily());
return multipartResolver;
}
}
2: Configure upload file parameters
### 配置springboot自动生成StandardMultipartResolver对象设置开始 ###
# 指定文件上传文件夹
spring.servlet.multipart.location=/Users/xb/Desktop/D/dongsir-dev/test-jsp-in-springboot
# 限制单个文件的大小 5M
spring.servlet.multipart.max-file-size=5242880
# 限制所有文件的最大大小
spring.servlet.multipart.max-request-size=20MB
### 配置springboot自动生成StandardMultipartResolver对象设置结束 ###
3:jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form method="post" action="./request" enctype="multipart/form-data">
<input type="file" name="file" value="请选择要上传文件">
<input type="submit" value="提交">
</form>
</body>
</html>
4: Jump jsp interface
@RequestMapping("/upload/page")
public String uploadPage() {
return "upload";
}
5: Interface upload request
HttpServletRequest is used directly as a parameter here, and you can also use the javax.servlet.http.Part
direct pass write
method. This is the interface provided in the servlet API, and you can also use it org.springframework.web.multipart.MultipartFile
. This is provided by spring. This transfterTo
method can be used directly . The source code is as follows:
@RequestMapping("/upload/request")
public Map<String, Object> uploadRequest(HttpServletRequest request) {
Map<String, Object> res = new HashMap<>();
MultipartHttpServletRequest multipartHttpServletReq = null;
if (request instanceof MultipartHttpServletRequest) {
multipartHttpServletReq = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartHttpServletReq.getFile("file");
String originalFilename = multipartFile.getOriginalFilename();
res.put("上传文件名", originalFilename);
res.put("上传结果", "成功");
try {
File file = new File(originalFilename);
multipartFile.transferTo(file);
} catch (Exception e) {
}
return res;
} else {
res.put("result", "不是文件上传请求");
return res;
}
}
6: Upload test
Just visit and http://localhost:8089/testBean/upload/page
select the file to upload.