Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Struts Struts2 Struts 2 File Upload

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

44 comments so far (post your own) View All Comments Latest 10 Comments:

For solving this you need to have two jar files in classpath. commons-fileupload-1.1.jar and commons-io-1.4.jar
Hope this will sove your problem

Posted by lucky on Tuesday, 10.19.10 @ 16:48pm | #100370

hi frnds i m having a problem here while running this progam

error is

Invalid field value for field "upload".

please resolve my problem

Posted by raju on Wednesday, 10.6.10 @ 09:51am | #100071

Thanks you for posting how to get the file filename.
It solves my problem.
Plesase post like this type tricks and unknown types


Posted by Naveenkumar.R on Tuesday, 02.23.10 @ 15:06pm | #95067

While uploading file using struts2, need to follow naming conventions to get the filecontent type and file name.

Here is notation

File upoload
String uploadFileName
String uploadContentType

and associated getter and setter methods.


Posted by Veeru on Monday, 02.22.10 @ 11:34am | #95030

you should add what libraries must be added for this tutorial to work


Posted by Stolarczyk Piotr on Wednesday, 02.17.10 @ 20:26pm | #94914

Hi i am getting the same error. however the null is not printed for small files. for huze sized files i am getting null when i print the file object after uploading

Posted by rafeeq on Tuesday, 11.24.09 @ 12:56pm | #92822

Hi roseindia team,

I have tried the example which is provided in this tutorial but all the values are coming as null.

Could you please me some tip where the mistake will be?


Posted by Thamayanthi on Friday, 11.20.09 @ 06:07am | #92730

Hi i need to get a properties file on application load up in my login jsp. Currently I am using request.getRealPath for this. But this is a dprecated method. However when the application is in expanded form, it seems to work just fine. But when I create a WAR file. The application is unable to get the path and thus unable to read the file. The application thus crashes at the very begining itself when deployed using a war file. How can it be resolved?

Posted by RabiKant Das on Wednesday, 11.18.09 @ 10:57am | #92663

I wanna upload image or any format of file on my server,i mean it should get copied on server location and should be displayed physically over there without deleting it.
i've a query regarding this code's issue,here The File Upload Interceptor actually deletes the the upload,then how can i see/download that file from the server?please suggest me the way to achieve my requirement and solve my query..


Posted by Barkha Jasani on Tuesday, 11.10.09 @ 14:58pm | #92468

sir, iwant to upload image in sql useing servlet.

Posted by sanjeev on Monday, 11.9.09 @ 19:27pm | #92447

FileUtils.copyFile(upload, theFile)

what is this, please mention and how do work.

Posted by dharmenra kumar on Friday, 07.31.09 @ 14:45pm | #89734

Can some one please tell me how to make both things.

1) it should throw exception if file size more than 2 mb.

But it dont throw the exception mentioned above. It should say error message given in the property file with property:struts.messages.error.file.too.large

Also, currently it throws exception that request was not valid, i am on edit page, so in that case i loose all the details that was there & the empty form comes :(.

Posted by amit on Tuesday, 07.28.09 @ 00:58am | #89627

Please help me
Struts2 file upload and Save

Posted by Harry on Monday, 05.11.09 @ 12:34pm | #87588

i want to uploading code in struts1.2 .i want cv send the database in struts1.2

Posted by sudhir on Sunday, 04.12.09 @ 18:35pm | #86745

I'm trying to do upload a file using struts2 file tag. Followed all the mentioned as given in http://struts.apache.org/2.0.11/docs/file-upload-interceptor.html. but still am not able to retreive the file contents in my action class. Below is the code snippet which i added.
struts.messages.error.uploading=a general error that occurs when the file could not be uploaded
struts.messages.error.file.too.large=occurs when the uploaded file is too large
struts.messages.error.content.type.not.allowed=occurs when the uploaded file does not match the expected content types specified

<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<interceptor-stack name="basicStack">
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="fileUpload"/>

<action name="uploadFile" method="uploadFile" class="com.example.MyAction">
<interceptor-ref name="fileUpload"/>
<exception-mapping exception="java.lang.Exception" result="error"/>
<result name="success">/jsp/success.jsp</result>

import java.io.File;
//The below properties with getter and setter methods defined
private File subUploadFile; // The actual file
private String uploadContentType; // The content type of the file
private String uploadFileName; // The uploaded file name and path

public String uploadFile() throws Exception {
System.out.println("Inside uploadFile() .....");
System.out.println("File : " + getSubUploadFile());
System.out.println("File Name : " + getUploadFileName());
System.out.println("Content type : " + getUploadContentType());
return SUCCESS;

<s:form action="goSubRegn" method="POST" enctype="multipart/form-data">
<s:file size="110" name="subUploadFile"/>

All the S.O.P. are printing null

Posted by SakthiPriya on Thursday, 04.9.09 @ 18:43pm | #86689

file upload send in database in struts1.2

Posted by sudhir on Thursday, 04.9.09 @ 13:36pm | #86679

I have the same issue as nutbag:
>>How do you set up a validator to make sure the user's file selection is valid? I've tried 'required' and an expression validator but none of them seem to catch it.

nutbag: did you figure this out? Does anyone know how?

Posted by rumblefish on Wednesday, 01.21.09 @ 15:37pm | #84027

Thanks. The tutorial was very useful and I had a basic file upload up and running in no time.

One very important piece of missing information however: you need to include the commons-filupload-1.2.jar and commons-io.1.3.1.jar files otherwise it won't work. This will fix the issue that people are having with "Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest" errors.

Other useful items. In order to change where the files get downloaded to, add a line to your struts.properties file. For example:


Finally, the file is deleted once the operation finished. In order to store it add some code to the "execute" method. For example:

public String execute() throws Exception {

File outFile = new File("/tmp/newfilename");
InputStream inStrm = new FileInputStream(getUpload());
OutputStream outStrm = new FileOutputStream(outFile);

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = inStrm.read(buf)) > 0) {
outStrm.write(buf, 0, len);

return SUCCESS;

Posted by Marcus on Friday, 01.16.09 @ 07:06am | #83879

i executed what ever givin in above example..
but no valuse diaplay

File Upload Example
Content Type:
File Name:
File Caption:

for this put the jar file name commons-fileupload-1.1.1.jar in wars lib.

Posted by anshul on Wednesday, 01.14.09 @ 02:13am | #83803

Really useful

Posted by any on Thursday, 12.11.08 @ 03:26am | #82639

I tried the code given above without any modifications. but iam getting error in input page like "Invalid field value for field "upload".

Posted by Chandrakantht on Friday, 11.28.08 @ 01:13am | #82130

I am not able to get the contents for the csv file but for other files its working fine.

Can you please help me

Posted by Sridhar on Monday, 11.3.08 @ 17:32pm | #81492

Hi all,

I did that,I have common-fileupload 1.0 and commons-io 1.3 and struts.properties file but it has an error that is:

javax.servlet.ServletException: Filter execution threw an exception

root cause

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext

I don't no why, please help

Posted by Vuong on Friday, 10.31.08 @ 15:54pm | #81448

What is the maximum size of file which I can upload?
Is it infinity means it takes more time, but will be able to upload?

Posted by Mukesh on Tuesday, 10.28.08 @ 20:05pm | #81371

I am new to Struts 2, The article provided here was very usefull. I have a requirement that the directory path(where the file to be uploaded in the server) should not be coded in action class. That has to be passed as parameter to action class from a xml file(not to be struts.xml).

Can i have the solution for this with an example please?

Posted by Balaji on Thursday, 10.23.08 @ 18:46pm | #81275

i am having the same problem as u.
can u guide me pls?

Posted by marco on Thursday, 10.16.08 @ 12:39pm | #81116

File upload time.. get null value, place the code in first of fillter mapping.. (web.xml)

<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class>

its may help you

Posted by Kannan on Wednesday, 10.1.08 @ 16:26pm | #80823

this code will never work.. it will never upload file.. this is fake

Posted by xaero on Monday, 06.30.08 @ 17:31pm | #65116

I want to make a JSP program to upload a file to specific location on the server, and also program to delete a file on server.please help with any kind source.

Posted by prithvi on Monday, 06.23.08 @ 13:53pm | #64310

why File Upload Interceptor deletes file and how to escape from it because for me this does not makes any sense that file gets deleted automatically then what is the point of uploading it..

Posted by xaero on Tuesday, 06.17.08 @ 16:26pm | #63615

This is the problem Iam getting when Iam running your file upload example
Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
How to solve it

Posted by febin on Monday, 04.28.08 @ 17:35pm | #58064

Could anyone show me how to upload more than one file?

Many thanks!

Posted by kienjudo on Tuesday, 03.4.08 @ 09:00am | #51251

Hi friends.....
i tried uploading all type of files....
now i want to upload only pictures of jpeg format......
Can u suggest me a solution?

Posted by srinadh on Wednesday, 02.20.08 @ 12:09pm | #49186

If you want to upload files bigger than 2mb in your struts.properties file, you must set this attribute


This value defaults to 2mb, if you want your fileupload interceptor to to manage max file size you must set this value to be larger than the interceptor setting.

And if you don't know... here is how you set it from the interceptor

<interceptor-ref name="fileUpload">
<param name="maximumSize" >1024</param>
<param name="allowedTypes">image/png,image/gif,image/jpeg,image/jpg</param>

Just put those lines inside of your action definition in your struts.xml.

Posted by trevis on Tuesday, 02.19.08 @ 22:32pm | #49113

As was said below, you need to download commons-fileupload and commons-io and add them to your lib folder.

But for it to work you also need to have a struts.properties file and add this line to it:


Posted by trevis on Thursday, 02.14.08 @ 11:42am | #48232

i executed what ever givin in above example..
but no valuse diaplay

File Upload Example
Content Type:
File Name:
File Caption:

any problam in above code given in dis page

Posted by raju on Saturday, 01.26.08 @ 15:32pm | #46235

Hi, I have a page with 2 submit buttons: one to upload the file and one to view the file. The "view button" reload the page to update a table. My problem is that when I reload the page, the path of the selected file isn't visible anymore and I think is a little confuse to the user.

Can you give some me help with this?

Posted by Adriana on Wednesday, 01.2.08 @ 18:39pm | #44371


I've done all the stuff necessary to save the file I uploaded but the one thing I can't figure out is how to save to a specific directory. Can someone please help?

Posted by Gau on Friday, 12.7.07 @ 09:09am | #41497

Pleas tell me how to upload the image file in struts2 . i am try allot of but i am facing problem. i file can upload but ......tmp file
and diffrent dir. how to write Action class or execute method pleas tell me .
and how to use interceptor .

Posted by rakesh on Tuesday, 10.30.07 @ 21:48pm | #35183

put struts.properties file in you WEB-INF/classes folder with the following entry:

Posted by Juby Rajan on Monday, 09.10.07 @ 19:18pm | #26600

kim, i am having the same problem as u.
have u get ot solved? can u guide me pls?

Posted by yy on Wednesday, 08.29.07 @ 22:00pm | #24442

I got an error with follow message.
Is there anybody knows how to solve this problem?
When i upload a file smaller than 2MB, it works so well-!!
Plea~~~~~se~~~~ help me.
I wanna destroy my laptop now...
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (5096543) exceeds the configured maximum (2097152)

I attache pieces of my code.
## Struts.xml

<interceptor-stack name="authenticationStack">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
<param name="maximumSize">
<interceptor-ref name="basicStack"/>


<action name="RegisterUser" class="authentication.RegisterUser">
<interceptor-ref name="authenticationStack"></interceptor-ref>

## InputForm.jsp
<s:file name="photo" label="%{getText('user.photo')}" accept="image/*" />

## ActionClass.java
private File photo;// Client side file
private String photoContentType; // The content type of the file
private String photoFileName; // Server side file name

public File getPhoto() {
return photo;

public void setPhoto(File upload) {
this.photo = upload;

public String getPhotoContentType() {
return photoContentType;

public void setPhotoContentType(String uploadContentType) {
this.photoContentType = uploadContentType;

public String getPhotoFileName() {
return photoFileName;

public void setPhotoFileName(String uploadFileName) {
this.photoFileName = uploadFileName;



Posted by Kim on Tuesday, 08.14.07 @ 23:04pm | #23392

How do you set up a validator to make sure the user's file selection is valid? I've tried 'required' and an expression validator but none of them seem to catch it.

Posted by nutbag on Thursday, 08.2.07 @ 08:23am | #22466

The file upload does not work unless you obtain commons-fileupload and commons-io and place them in the web-inf/lib folder of the tutorial.

Posted by John on Sunday, 07.1.07 @ 18:19pm | #20525

Ask programming questions?



Back to Tutorial


  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.