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.
My self vikas working in JAVA/J2ee
i want one realtime project can you provide it
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.
Hi @Mihir could you plz help me with multiple file upload code…
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.
how did u fix it ??
Sorry I’ve already fix it!
Very Good!!!!!!!!!!!!!!!!
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
photo = rs.getBlob(“photo”);
mgData = photo.getBytes(1, (int) photo.length());
response.setContentType(“image/jpeg”);
OutputStream o = response.getOutputStream();
rs.getBlob(“photo”);
rs.getBinaryStream(“photo”);
what is the use of multipart/form-data
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 !!!!!
i want the coad for uploading 3 files on submitting a form. can u help me with this..
thanks in advance.
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.
I forgot to mention,
I require the code using struts
Not servelets or hybernate
Thanx it is really useful