Solved

Java Spring File Upload gives file upload size exception

Posted on 2013-11-12
6
67 Views
Last Modified: 2016-07-11
I am using java/spring version before 3.0.   I am setting the file upload multipart resolver in servlet.xml as such below:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxInMemorySize" value="5250000"></property>
            <property name="maxUploadSize" value="2048000"/>  
</bean>

I then have my files as normal in the jsp:

<spring:bind path="jobs.file1">
&nbsp;<input class="input-box" name="file1" id="f1" type="file"  />
      <c:if test="${fn:length(status.errorCode) > 0}">
      <span class="error"><c:out value="${status.errorCode}"/></span>
      </c:if>
</spring:bind>

However, what I am finding is that if the user uploads a file greater than the 2048000 byte maximum, an error occurs that I cannot trap as it occurs even before the validator is fired.  How does one validate for this error?
0
Comment
Question by:shdmuser
  • 3
6 Comments
 
LVL 26

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 39645212
File upload is not great on checking the file before it is uploaded.  Because of the sandbox on the user side, it can be difficult or simply not possible to validate file size before the file is uploaded to the server.

On the server side, there are several places where file size becomes an issue:
* server disk space -- the entire file is fully uploaded to the server with the HTML "file" type so you have to have enough disk space available to the machine for the upload
* Web server file size limit -- most web servers have a default max size of file which they will allow.  This often doesn't work wonderfully, since the entire file is uploaded to some temp area (see previous comment), and then an exception or error is thrown in the web server saying that the file is too large.  Or it silently fails.  Depends on the web server.
* your servlet code.  At this point, you can check the size of the file.  If it's larger than you want, you can return an error message to the user.

Web servers usually have a config parameter to allow larger file sizes, if that's what you want.  Your OS might be failing silently on large files, although 2 mb doesn't sound as if it would be a problem for that.

If you successfully get to your servlet, then you can check the file size with something like this:
long sizeInBytesInit = item.getSize();
if( sizeInBytesInit > myMaxFileSize ) { /* process error here */}
0
 

Author Comment

by:shdmuser
ID: 39659746
Problem is the error occurs before validation or onsubmit can happen, so I do not get this option if I turn on:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxInMemorySize" value="5250000"></property>
            <property name="maxUploadSize" value="2048000"/>  
</bean>

If I turn this off I can get to the exception, but then all the spring parsing goes out the window.
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 39660402
Yes.  Spring and other frameworks can be helpful, but they prevent full use of the technology by building layers for newbies.

So -- you will have to write actual Java code to handle the file upload if you want to debug this situation.  In the end, Spring's implementation of file upload leaves a lot to be desired, so you might want to permanently rewrite your file upload to use the actual direct http technology rather than Spring's layer.  These open-source frameworks don't have the revenue stream to develop good APIs for every corner of HTTP and HTML.
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 41704289
The answer is correct, and this is the kind of thing that anyone using a servlet engine (or really, any web server) will hit.  It's a useful answer as well as correct.

Assign points to
mrcoffee365 2013-11-13 at 11:20:08 ID: 39645212
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now