?
Solved

Java Spring File Upload gives file upload size exception

Posted on 2013-11-12
6
Medium Priority
?
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 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