我正在使用 Spring Boot 来测试上传功能,并收到“所需的 MultipartFile 参数‘文件’不存在”错误。 当它部署外部tomcat服务器时。 但它可以与 spring boot tomcat 插件正常工作 以下是 1) JSP、2) Controller 3) 配置属性
1)
<form id="initialUploadForm" action="${root}/upload/uploadCapFile" enctype="multipart/form-data" method="post">
<table align="left" width="50%" cellspacing="0" cellpadding="5"
border="0" class="formTable">
<tr>
<td><label class="">Add Cap File : </label></td>
<td><input type="file" name="file" style="width:100%;"/></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td><input class="btn btn-success" type="submit"
value="Upload Data" id="btnInitialUpload"/></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
2)
@RequestMapping(value = "uploadCapFile", method = RequestMethod.POST)
ModelAndView uploadCapFileData(@RequestParam("file") MultipartFile file, Model model) {
try {
if (log.isDebugEnabled()) log.debug("UploadController calling : ");
System.out.println("File Name : " + file.getOriginalFilename());
long lStartTime = new Date().getTime();
if (!file.isEmpty()) {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (extension.equals(Constants.FILE_FORMAT)) {
File convFile = convertFile(file);
InputStream in = new FileInputStream(convFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
if (isProdUpdate == 1) {
hotelService.updateAgentMarkup(out.toString());
} else {
hotelService.updateHotelData(out.toString());
}
} else {
log.error("Invalid File Format ! File Format is :" + Constants.FILE_FORMAT);
}
} else {
log.error("File is missing ! Can not be Process");
}
long lEndTime = new Date().getTime();
long output = TimeUnit.MILLISECONDS.toSeconds(lEndTime - lStartTime);
processingTime = String.valueOf(output);
} catch (IOException e) {
log.error("Error occurred while calling the UploadController : " + e);
} catch (TalcacheException e) {
log.error("Error occurred while calling the UploadController : " + e.getErrorMsg());
} catch (Exception e) {
log.error("Error occurred while calling the UploadController : " + e);
}
}
3)
spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=5KB
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=215MB
最佳答案
将此代码放入配置文件
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(-1);
return multipartResolver;
}
关于java - Tomcat : Required request part 'file' is not present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53064265/