Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Spring FileUpload

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.

like image 838
TheJediCowboy Avatar asked Sep 19 '10 20:09

TheJediCowboy


1 Answers

After doing a lot of research, I solved my problem. It turns out that there is not a default limit set for the maximum amount of bytes that you can upload using CommonsMultipartFileResolver Of course you can specify in your bean whatever you want for this amount by setting the following property.

<property name="maxUploadSize" value="99999999999"/>

There is also a property maxInMemorySize that allows you to specify the maximum size allowed before files are written to disk. Although this works the same way as the max upload size, if you do not specify an amount, it defaults to 1024 bytes. This would explain it breaking if I attempted to upload a large file.

In order to allow files above 1024 bytes to be uploaded, you need to increase the maxInMemorySize value to whatever you need like the following...

This is what took care of my problem. I learned that this property defaults to 1024 when I was reviewing documentation for CommonsFileUpload Documentation.

You can view this documentation at CommonsFileUpload Documentation

I hope this helps anybody, as there is not very good documentation on using CommonsMultipartFile.

like image 95
TheJediCowboy Avatar answered Sep 22 '22 11:09

TheJediCowboy