Struts 2 File Upload & Save Example

Struts » On Nov 8, 2011 By Sivateja

Let us see how to work with file uploads in struts 2 frame work, things to remember while working with this type of application

  • See in index.jsp i have taken <s:file  name=”uploadFile” i mean my file tag name is uploadFile
  • We used to write setters, getters for this property in Action class right, but while writing we need to write setters, getters for 2 more properties with names uploadFileFileName  &  uploadFileContentType
  • I mean name format must be [ our file property name ]ContentType & [ our file property name ]FileName
  • Actually at run time struts 2 container will injects the required file details into these properties, and thing is see struts.xml –  line 23 fileUpload is the predefined interceptor class, this will take cares every thing
  • Once file uploaded, struts 2 will stores the file with some temp name, its our responsibility to convert and save that file,  see line numbers 39,40,41,42 in Action class of

File Upload Example

required files….

  • index.jsp
  • success.jsp
  • web.xml
  • struts.xml

Directory Structure






Output If if we take wrong file

Output If file accepted


Java/J2EE Developer & Web Designer, founder of Sign-up for the email newsletter for your daily dose of Java tutorials.


19 Responses to “Struts 2 File Upload & Save Example”
  1. Merlene Standberry says:

    Well I really enjoyed reading it. This article offered by you is very constructive for accurate planning.

  2. saharh says:

    thanks for this.

    but if we want to check that only .txt or .csv is uploaded then how to validate the extension..

  3. Java4s says:


    Actually in the current example i have given

    <param name=”allowedTypes”>text/plain</param>

    to allow text related files only. When the uploaded file type does not match one of the MIME types specified a field error will be created.

    If that is image, you can give

    <param name=”allowedTypes”>image/jpeg</param>


    <param name=”allowedTypes”>image/gif</param>

    its depends.

  4. Bhaskar says:

    I am unable to find to retrieve the uploaded file,the filePath mentioned did not contain any concerned file!

  5. Java4s says:


    You will be able to find your uploaded file in ‘c:/Myuploads‘ location, however you can change the location.

  6. kanishka says:

    this is work how can we upload mp3 file format

  7. prabakaran says:

    could u plz do with oracle database because im facing with oracle db but not with mysql

  8. vaigha says:

    how to save the file in a different extension.Like i can upload both .xls & .xlsx but it should save in .xlsx format only.How can i do that?

  9. Amol Ubale says:

    Nice article to good support to my project for uploading file.

  10. Very useful for me.Thank you so much.

  11. shakti singh says:

    can u please post a tutorial on struts 2 preloaded interface and service locator… Thnx in advance :P

  12. munna says:

    please reply me how to add datetimestamp to filename while uploading file.?????

  13. niraj says:

    i upload .csv file and i got .tmp file.. so how could i get my original file (.csv).

  14. Pavan says:

    Can you explain a code uploading image to mySQL database using struts2, WITHOUT any tiles or hibernate ingration. Thankyou.

  15. Sudeetha Nuwan says:

    application/ use for upload csv and another ms excel related formats.

  16. sankar says:

    very nice examples easy to under stand thank you so much

  17. k says:

    The image uploaded is not accepted..its showing null

  18. Subrahmanyam says:

    File upload interceptor is not working fine in ie if i give valid file format also not accepting

  19. chikku says:

    org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NullPointerException


By posting your answer, you agree to our comments policy.
© 2010 - 2016
Java4s - Get It Yourself.
The content & design is copyrighted to Sivateja Kandula, it may not be reproduced on other websites.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.