使用Tomcat在Spring中上传文件与允许的最大大小有关
我对Spring相当陌生,我想拥有一个多部分表单并处理MaxUploadSizeExceededException异常,以便在jsp中显示错误消息。我遇到的主要问题是MaxUploadSizeLimitExceededExceptionResolver类中的ModelAndView对象,我不知道如何将其用于上述目的。
我拥有的文件:1)模型类UploadItem.java。2)查看表单UploadForm.jsp。3)控制器Uploadcontroller.java。4)类MaxUploadSizeLimitExceededExceptionResolver.java处理异常Uploadcontroller
1)模型UploadItem
public class UploadItem { private String name;
private CommonsMultipartFile fileData;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CommonsMultipartFile getFileData() {
return fileData;
}
public void setFileData(CommonsMultipartFile fileData) {
this.fileData = fileData;
}
}
2)查看表单UploadForm.jsp
<html> <head>
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Upload form</title>
</head>
<body>
<form:form modelAttribute="uploadItem" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Upload Fields</legend>
<p>
<form:label for="name" path="name">Name</form:label><br/>
<form:input path="name"/>
</p>
<p>
<form:label for="fileData" path="fileData">File</form:label><br/>
<form:input path="fileData" type="file"/>
</p>
<p>
<input type="submit" />
</p>
</fieldset>
</form:form>
</body>
</html>
3)控制器上传控制器
public class Uploadcontroller { @RequestMapping(method = RequestMethod.GET)
public String getUploadForm(Model model) {
model.addAttribute(new UploadItem());
return "upload/uploadForm";
}
@RequestMapping(method = RequestMethod.POST)
public String create(HttpServletRequest request, UploadItem uploadItem,
BindingResult result, Exception exception) {
if (result.hasErrors()) {
// logger.info("create upload");
for (ObjectError error : result.getAllErrors()) {
System.err.println("Error: " + error.getCode() + " - "
+ error.getDefaultMessage());
}
return "upload/uploadForm";
}
System.err.println("Test upload: " + uploadItem.getName());
System.err.println("Test upload: "
+ uploadItem.getFileData().getOriginalFilename());
// TODO redirect this
return "/home";
}
}
4)处理异常的组件Uploadcontroller
@Componentpublic class MaxUploadSizeLimitExceededExceptionResolver extends
SimpleMappingExceptionResolver {
private static final Logger logger = LoggerFactory
.getLogger(HomeController.class);
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception) {
Map<String, Object> model = new HashMap<String, Object>();
logger.info("getUploadForm at the beggining");
ModelAndView modelView = new ModelAndView();
if (exception instanceof MaxUploadSizeExceededException)
{
model.put("errors", exception.getMessage());
modelView.addObject("errors", exception.getMessage());
logger.info("getUploadForm: MaxUploadSizeExceededException" );
} else
{
model.put("errors", "Unexpected error: " + exception.getMessage());
modelView.addObject("errors", "Unexpected error: " +exception.getMessage());
}
logger.info("getUploadForm at the end" );
model.put("uploadedFile", new UploadItem());
modelView.addObject(new UploadItem());//("uploadedFile", new UploadItem());
modelView.setViewName("/upload/uploadForm");
return modelView;
}
}
编辑以添加更多详细信息:实际上,问题出在另一个方向。我的maxUploadSize文件的大小设置为1MB,但是我试图对大于3
MB的文件进行测试。当我尝试使用一个文件直到最大2
MB正常工作时。问题是我得到了ERR_CONNECTION_RESET,似乎与Tomcat有关,它是maxSwallowSize的配置->stackoverflow.com/questions/29113262/…。我将继续研究并保持该线程更新。
新的消息。我尝试使用Tomcat 7.0.61,错误是ERR_CONNECTION_ABORTED我尝试使用Tomcat 6.0.43,没有错误!
回答:
经过调查,问题出在Tomcat。
在版本7.0.55中引入了属性maxSwallowSize,默认情况下将其设置为2MB。这使得Tomcat中止了上载请求。通过将此属性设置为另一个值,可以解决问题(我将其更改为-1,请不要在您的PRD环境中执行此操作,因为Tomcat确实会尝试获取要上传的X
MB文件)。为此,我在连接器属性的Tomcat服务器文件$ {tomcat_dir}
/conf/server.xml中添加了属性maxSwallowSize
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443"
maxSwallowSize="-1"/>
您需要重新启动Tomcat,以便它采用此配置,如果不起作用,请删除服务器并再次添加。
以上是 使用Tomcat在Spring中上传文件与允许的最大大小有关 的全部内容, 来源链接: utcz.com/qa/414588.html