You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

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 LogingEx.java

File Upload Example

required files….

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

Directory Structure

index.jsp

success.jsp

LogingEx.java

web.xml

struts.xml

Output

Output If if we take wrong file

Output If file accepted

 






By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja [at] java4s.com
Follow me on


Comments

15 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:

    @Saharh

    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>

    [or]

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

    its depends.

  4. Bhaskar says:

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

  5. Java4s says:

    @Bhaskar

    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/vnd.ms-excel use for upload csv and another ms excel related formats.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.