我对Spring相当陌生,我希望有一个多部分的表单,并处理MaxUploadSizeExceededException异常,以便在jsp中显示错误消息。我遇到的主要问题是类ModelAndView中的MaxUploadSizeLimitExceededExceptionResolver对象,我不知道如何将它用于前面解释过的目标。
我拥有的文件: 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)控制器Uploadcontroller
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的组件。
@Component
public 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,但我试图对大于3MB的文件进行测试。当我尝试一个文件,直到最大2MB的工作正常。问题是我得到了一个ERR_CONNECTION_RESET,它似乎与maxSwallowSize ->stackoverflow.com. The /…/29113262/…的配置有关我将继续研究和保持这条线更新。
新信息。我尝试过使用Tomcat 7.0.61,错误是ERR_CONNECTION_ABORTED,我尝试过使用Tomcat 6.0.43,没有错误!
发布于 2015-04-19 12:20:27
经过调查,这个问题是由Tomcat引起的。
默认情况下,在7.0.55版本中引入了属性maxSwallowSize设置为2MB。这使得Tomcat中止了上传请求。通过设置此属性另一种价值,问题得到了解决(我将其改为-1,请不要在您的珠三角环境中这样做,因为Tomcat将真正尝试获得一个X文件以供上传)。为此,我在连接器属性maxSwallowSize属性中添加了Tomcat服务器文件${maxSwallowSize}/conf/server.xml。
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxSwallowSize="-1"/>
您需要重新启动Tomcat,以便接受此配置,如果不能工作,请删除服务器并再次添加它。
https://stackoverflow.com/questions/29684691
复制相似问题