File Upload in struts 2

This is 6 of 8 part of struts 2 tutorial.In this tutorial we will learn how to upload file to server in struts 2 and also how save uploaded file on directory of server machine.

Tutorial Content:

Struts 2 file upload component can be used to upload mulitpart file in struts 2 application.Struts 2 utilizes in built File upload interceptor for uploading files in struts 2 application.The Struts 2 File Upload Interceptor is based on MultiPartRequestWrapper, which is automatically applied to the request if it contains the file element.

Required jar :
We need to have 1 jar common-io.jar in your classPath.In configuring struts 2 in eclipse post,we have already added jar in classPath.
Create project named “fileUploadInStruts2”.For configuring struts 2 in your eclipse ide please refer configuring struts 2 link.Project stucture:

Action class:

For adding file uploading functionality in struts 2,We will create action class named FileUploadAction.java.
Create FileUploadAction.java file under src folder in package org.arpit.javaPostsForLearning.
copy following content in FileUploadAction.java
Now we have declared three attributes here

private File toBeUploaded  //Actual file to be uploaded
private String toBeUploadedFileName // file name of actual file to be uploaded
private String toVeUploadedContentType  // content type of actual file to be uploaded

We have provided getters and setters for above attributes.So struts 2 automatically set these value according to file being uploaded.
For example if your file name is “sampleFile” then struts 2 interceptor add three parameters to request.
Three parameters are:
[sampleFile]:file
[sampleFile]FileName:String
[sampleFile]ContentType:String

you can notice here that we have given our action class attributes in same format so it will automatically set these attributes according to file being uploaded.

One more thing is that we have implemented “ServletRequestAware” interface in our action class
This is to get ServletRequest object.This request object is required to get path of server machine where file is to be uploaded.

JSPs:

We will create two jsp files here.fileUpload.jsp will display form for uploading file.On clicking on upload button,File will be saved on server machine and request will be forwarded to uploadSucceed.jsp which will render fileName,content type and uploaded image.

fileUpload.jsp:

Create fileUpload.jsp under WebContent folder.
Copy following content to fileUpload.jsp.
   
   

We have used tag for file upload interface.

uploadSucceed.jsp:
Create jsp named “uploadSucceed.jsp” under WebContent folder.
Copy following content into uploadSucceed.jsp.

Create struts.xml under src folder.
Copy following content to struts.xml

Web.xml:

It is same as in previous posts except that we have changed welcome file to “fileUpload.jsp”
Copy following content into web.xml

Run project:

Right click on project ->run as->run on server
copy resultant url(http://localhost:8080/fileUploadInStruts2/) to your browser.
Now we will browse here text file and click upload.Following screen will appear
Now again copy http://localhost:8080/fileUploadInStruts2/ url to your browser and press enter
you will get first screen again.We will browse image file named “nature.jpeg” and click on upload.
you will get following screen

Source code:

Now in next post,we will write struts 2 ajax example.

Was this post helpful?

Leave a Reply

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