application.yaml
multipart 配置,设置接受文件的保存路径和文件大小
spring:
servlet:
multipart:
enabled: true
location: F:/tmp/
file-size-threshold: 5MB
max-file-size: 20MB
UploadConfig.java
@Configuration
// 需要的类
@ConditionalOnClass({Servlet.class, StandardServletMultipartResolver.class, MultipartConfigElement.class})
// 需要的配置
@ConditionalOnProperty(prefix = "spring.http.multipart", name = "enable", matchIfMissing = true)
// 将 MultipartProperties 加入容器
@EnableConfigurationProperties(MultipartProperties.class)
public class UploadConfig {
private final MultipartProperties multipartProperties;
@Autowired
public UploadConfig(MultipartProperties multipartProperties) {
this.multipartProperties = multipartProperties;
}
// Servlet 规范
// 定义了 http 服务上传文件存储位置、最大文件大小、最大请求的长度
@Bean
@ConditionalOnMissingBean
public MultipartConfigElement multipartConfigElement() {
return this.multipartProperties.createMultipartConfig();
}
// SpringMVC 文件解析器
@Bean(DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
@ConditionalOnMissingBean(MultipartResolver.class)
public StandardServletMultipartResolver multipartResolver() {
StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();
multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily());
return multipartResolver;
}
}
UploadController.java
@Controller
public class UploadController {
@Value("${spring.servlet.multipart.location}")
private String fileTempPath;
@ResponseBody
@PostMapping(value = "/local", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "File Empty!";
}
// 文件名
String fileName = file.getOriginalFilename();
// 路径
String filePath = this.fileTempPath + fileName;
File targetFile = new File(filePath);
try {
// 写入目标文件
file.transferTo(targetFile);
} catch (IOException e) {
return "TransferTo error";
}
return "Upload OK";
}
}