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 ></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.

View Comments

  • 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.

  • 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.

  • 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..

    • 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

  • 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.

  • Hi,

    The given example was working fine. i tested with many file forms and able to upload multiple files with fixed file control in html.
    but
    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.

  • Sir.,
    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

  • Hi,
    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
    Cri

Share
Published by
Viral Patel
Tags: Struts struts file upload Tutorial

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

4 years ago