Tutorial: Struts File Upload Example.

Let us see how we can implement file upload functionality using Apache Struts Framework. I assume you have basic knowledge about Struts and know the flow of a struts application. If you are new to struts, I suggest you to check this tutorial first: Struts Project in Eclipse. First let us download all the required JAR files to implement file upload functionality. For this we will need common-fileupload.jar file that comes with common package of Struts. Download common-fileupload.jar (version 1.0, size 20 kb). Include this JAR file in your class path variable. Now, modify the ActionForm class where you want to add File upload functionality and add following property and its getter and setter methods.
import org.apache.struts.upload.FormFile; .... .... private FormFile file; public FormFile getFile() { return file; } public void setFile(FormFile file) { this.file = file; } .... ....
Code language: Java (java)
In the above code snippet, we have added an attribute file which is of type org.apache.struts.upload.FormFile. This property will contain file content and other file related information that is being upload from form. Now add following code in your JSP page to add a file upload input.
<html:form action="/file-upload" method="post" enctype="multipart/form-data"> .... .... <html:file property="file"></html:file> .... .... </html:form>
Code language: HTML, XML (xml)
Note that in above code we added method=”post” and enctype=”multipart/form-data” to the form. Also the name of property is same as what we mentioned in ActionForm class. Now in the action file where you normally deal with form data, add following code to get information about the file being submitted.
//Type cast form to FileUploadForm. FileUploadForm fileForm = (FileUploadForm)form; //Get the FormFile object from ActionForm. FormFile file = fileForm.getFile(); //Get file name of uploaded file. String fileName = file.getFileName(); //Get file size of uploaded file. Integer fileSize = file.getFileSize(); //The content type of the uploaded file. String contentType = file.getContentType(); // Get InputStream object of uploaded File. InputStream inputFile = file.getInputStream();
Code language: Java (java)
Once you get InputStream object, you can use normal file I/O to write the file anywhere on server.
Get our Articles via Email. Enter your email address.

You may also like...


  1. Vikas Koshti says:

    My self vikas working in JAVA/J2ee

    • jayanthi says:

      i want one realtime project can you provide it

  2. Mihir Desai says:

    How about a multiple file upload using older struts jar.

    I already did a multiple file upload ( say 3 – 4 files) using just 1 upload button but looking for a better solution if possible.

    I used arraylist in actionform., if you have better solution It will be appreciated.

    • @Mihir, I haven’t looked into that direction yet. But I feel your solution (using arraylist in actionform) seems to be correct. I will update you if I come up with something better.

    • Zoya says:

      Hi @Mihir could you plz help me with multiple file upload code…

  3. Sol says:

    Thank you for your example; is my first time dealing with FileUpload FormFile manually.
    But I have a problem in my action class it is: “Cannot cast form ActionForm to FileUploadForm”
    And I have imported the common-fileupload.jar version 1.0
    Can you help me please? Do you know why?
    Thanks a lot in advanced.

    • PB says:

      how did u fix it ??

  4. Sol says:

    Sorry I’ve already fix it!

  5. Very Good!!!!!!!!!!!!!!!!

  6. Nirmal mehta says:

    I want to add one validation as how can i check that the uploaded file is in txt format and values in the file are one after the new line. Could you please help..

    • brahma says:

      well its simple. to add text file check the content type of the text and make validation like if(!file.getContentType().equalsIgnoreCase(“image/text”)){
      }like this u can validate your application or you can make your custom validations

  7. brahma says:

    well this example is good for storing the file into server.
    But the problem for me is that retrieving the image from database i am getting the stream of image. can any one tell me how to display image from database to jsp.

  8. mohanraj says:


    The given example was working fine. i tested with many file forms and able to upload multiple files with fixed file control in html.
    i need to upload multiple files using single input type file control.In html only one file control should be there,

    can please suggest the solution.

  9. Jrkkiran says:

    I was developed an Java struts application by using File Upload Concept.
    When I Upload the File,
    It was uploaded and it was saved on server location. I saved that real location into database.
    App developing by using POJO Class and Which is defined on ValidationForm Object.
    When I want that file on particular JSP File . So I need to convert the Java.io.File Object into FormFile
    Please Give the Information about

    How to convert the File into FormFile

  10. Cri says:

    thank you for your post.
    It’s good if there is only one field in the form, but what if validation fails for another field? I don’t succeed in retrieving the file.

    Thank you

  11. jaffa says:

    photo = rs.getBlob(“photo”);
    mgData = photo.getBytes(1, (int) photo.length());
    OutputStream o = response.getOutputStream();

  12. jayanthi says:

    what is the use of multipart/form-data

  13. Hemant says:

    I need a demo and complete web application with excel file uploading functionality end-to-end using DOJO toolkit in client side and it should be in struts1.2 MVC.

    It will be very helpful to learn the DOJO file upload functionality.
    I searched the web but I can’t find the the complete and success swample application.
    Kindly help me out.
    This site rocks $$$$$$$
    Thanks all for your help in advance !!!!!

  14. ankita says:

    i want the coad for uploading 3 files on submitting a form. can u help me with this..
    thanks in advance.

  15. Naman says:

    I need a code to upload a image and store it into a database, and then retrieve that image if the user enters the pic id and asks for it.

  16. Naman says:

    I forgot to mention,
    I require the code using struts
    Not servelets or hybernate

  17. deep says:

    Thanx it is really useful

Leave a Reply

Your email address will not be published. Required fields are marked *