Uploading files in Spring with Tomcat related to max size allowed
I am significantly new to Spring and I want to have a multipart form and handle the MaxUploadSizeExceededException to show the error message in the jsp. The main problem I am facing is the ModelAndView object in the MaxUploadSizeLimitExceededExceptionResolver class, which I donβt know how to use it for the kind of purpose that was previously explained.
The files I have are: 1) The UploadItem.java model class. 2) View the UploadForm.jsp form. 3) Controller Uploadcontroller.java. 4) MaxUploadSizeLimitExceededExceptionResolver.java class to handle Uploadcontroller exception
1) Model 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) View Form 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) Controller boot controller
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) Component for handling Uploadcontroller exception
@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;
}
}
Edit to add more details: Actually the problem is in the other direction. My maxUploadSize was set to 1MB, but I tried to run tests with files larger than 3MB. When I try with a file while max 2MB works fine. The problem is I am getting ERR_CONNECTION_RESET and it seems to have something to do with Tomcat, maxSwallowSize config -> stackoverflow.com/questions/29113262 / ... I will continue exploring and updating this thread.
New information. I tried with Tomcat 7.0.61 and the error is ERR_CONNECTION_ABORTED I tried with Tomcat 6.0.43 and there is no error!
source to share
After investigating, the problem occurred due to Tomcat.
In version 7.0.55, the maxSwallowSize property was introduced , which is set by default to 2 MB. This caused Tomcat to refuse the download request. Setting this attribute to a different value solved the problem (I changed it to -1, please do not do this in your PRD environment as Tomcat will actually try to download the X-MB file). I did this by adding to my Tomcat server file $ {tomcat_dir} /conf/server.xml to my connector property the maxSwallowSize attribute
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxSwallowSize="-1"/>
You need to restart Tomcat to keep this configuration from running, if not working, remove the server and add it again.
source to share