dimanche 19 avril 2015

Uploading files in Spring with Tomcat related to the maximum size allowed

I am considerably new with Spring, and I want to have a multipart form and handle the MaxUploadSizeExceededException exception in order to show an error message in the jsp. The main problem I have is the ModelAndView Object in the class MaxUploadSizeLimitExceededExceptionResolver, that I don't know how to use it for such objective previously explained.


Files that I have: 1) Model class UploadItem.java. 2) View form UploadForm.jsp. 3) Controller Uploadcontroller.java. 4) Class MaxUploadSizeLimitExceededExceptionResolver.java to handle the exception Uploadcontroller


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 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) Component to handle the exception 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;
}
}




Edit to add more details: Actually, the problem is in another direction. The size of my maxUploadSize file was set to 1MB, but I was trying to do tests with files bigger than 3 MB. When I try with a file until 2 MB maximum is working fine. The problem is that I get a ERR_CONNECTION_RESET and it seems something related to Tomcat, a config of maxSwallowSize --> http://ift.tt/1b9m6ud I will continue researching and keep this thread updated.




New information. I have tried with Tomcat 7.0.61 and the error is ERR_CONNECTION_ABORTED I have tried with Tomcat 6.0.43 and there is no error!


Aucun commentaire:

Enregistrer un commentaire