Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Spring File Upload gives file upload size exception

Posted on 2013-11-12
6
Medium Priority
?
129 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

636 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