I have the following block of code which is handling my file upload of a photo that I am using in my Spring MVC web application. I am using Spring MVC CommonsMultipartFileResolver to handle file uploads.
if(model.getPhoto() != null){
if(!model.getPhoto().isEmpty()){
MultipartFile file = model.getPhoto();
String fileName = file.getOriginalFilename();
String filePath = baseDirectory + fileName;
FileOutputStream fos = new FileOutputStream(filePath);
try
{
fos.write(file.getBytes());
agentProfile.setPhotoUri(fileName);
}
catch (IllegalStateException e)
{
System.out.println(e);
}
finally
{
fos.close();
}
}
}
In my app-servlet.xml file I have the following code to configure the MultipartFile resolver bean.
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
I am experiencing some random problems when I am uploading photos.
1)If I go to upload a smaller photo, around 3 kb or so, It will upload successfully.
2)If I go to upload a little larger photo, it will create the file in the directory, but with a size of 0 bytes and will give the following error message.
java.lang.IllegalStateException: File has been moved - cannot be read again
org.springframework.web.multipart.commons.CommonsMultipartFile.getBytes(CommonsMultipartFile.java:112)
com.mmz.admin.mvc.controller.AddAgentController.processFinish(AddAgentController.java:145)
org.springframework.web.servlet.mvc.AbstractWizardFormController.validatePagesAndFinish(AbstractWizardFormController.java:642)
org.springframework.web.servlet.mvc.AbstractWizardFormController.processFormSubmission(AbstractWizardFormController.java:492)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:874)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:808)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:441)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I have tried a couple different options configuring the Multipart resolver such as switching it to handle a CommonsMultipartFile object as oppose to a plain MultipartFile object, but nothing changed.
I have also tried to manuall configure the maximum upload size in the CommonsMultipartFileResolver bean with the following property.
<property name="maxUploadSize" value="1024000000"/>
nothing changed as well. I am not sure what the CommonsMultipartResolver defaults to as far as size of the file that can be uploaded, but that is not my question.
I have been told that the problem I am experiencing is due to a problem in the Multipart parser/handler that spring is using. I had a recent post about this same problem, and because new information was found, wanted to repost with the new information. The old post can be found at CommonsMultipartFileResolver Problem
I feel that I have checked nearly every resource on the internet to find additional documentation, but am unable to figure out the problem.
Please help me figure out what is going on with this, and if there is a better, simpler solution to maybe explorer those options, but I would prefer to stay with my current method if I can find a solution.
EDIT
Note- I have been experimenting with different size photos to upload, and I believe that the limit that it is allowing me to upload is around 10Kb. Anything larger then 10Kb is causing it to break and give me the error above.
See Question&Answers more detail:
os