Solved

Java Spring File Upload gives file upload size exception

Posted on 2013-11-12
6
82 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 27

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 27

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 27

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java DateChooser? 3 36
why cannot we forward request once the response is committed 2 42
table example 4 29
java stored proc example 9 32
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

810 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