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
Solved

Java Spring File Upload gives file upload size exception

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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