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. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
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