aspose file tools*
The moose likes Struts and the fly likes Reading the content of the uploaded file in Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Reading the content of the uploaded file in Struts2" Watch "Reading the content of the uploaded file in Struts2" New topic
Author

Reading the content of the uploaded file in Struts2

Damodar Wagle
Greenhorn

Joined: May 18, 2011
Posts: 5

Can any one please give me some sample code in the Execute method of the Action class or some direction as to how do I read the content of the Uploaded File. I am able to upload the file on the server but do not know how to read the content of that file.

Do we have to first save it on the server ? Or can we read it directly?

Please help !!!


Thanks and Regards
Damodar
Sivakumar Subburaman
Greenhorn

Joined: Jun 10, 2011
Posts: 13
Hi,

I assume that you are using FormFile class for the upload file control.

Here is the sample to read the data from that form component and convert that into String.

FormFile uploadFile = frm.getUploadFile();
int size = uploadFile.getFileSize();
char[] theChars = new char[size];
byte[] bytes = uploadFile.getFileData();

for (int i = 0; i < size;)
theChars[i] = (char)(bytes[i++]&0xff);

String fileContent = new String(theChars);

Hope this helps!
debraj mallick
Ranch Hand

Joined: Mar 08, 2011
Posts: 188


once you have upload the file to your desire location, use the org.apache.commons.io.FileUtils, take a look at the link below
http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html
hope this would help you.
Nirmal singh Mehta
Greenhorn

Joined: Sep 08, 2012
Posts: 6
I want only the txt file to be uploaded and that should contain email addresses with one after the new line. Please help me to validate that an after that how to get the email addresses in array of string using struts...Please help me
Nirmal singh Mehta
Greenhorn

Joined: Sep 08, 2012
Posts: 6
Nirmal singh Mehta wrote:I want only the txt file to be uploaded and that should contain email addresses with one after the new line. Please help me to validate that an after that how to get the email addresses in array of string using struts...Please help me
Nirmal singh Mehta
Greenhorn

Joined: Sep 08, 2012
Posts: 6
Nirmal singh Mehta wrote:
Nirmal singh Mehta wrote:I want only the txt file to be uploaded and that should contain email addresses with one after the new line. Please help me to validate that an after that how to get the email addresses in array of string using struts...Please help me
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

Hi Nirmal,

You can get the code for file uploading using struts very easily .Second think if i understand the problem correctly then you need to validate the email addresses while uploading the file right?

Then you need to put validation when you are reading file line by line for the uploading. Also show some efforts to code definitely some one will help you.

Regards
Jatan
Nirmal singh Mehta
Greenhorn

Joined: Sep 08, 2012
Posts: 6
Hi Jatan,

Thanks for the reply. I got the solution for file upload but the main thing i want to know is like how to validate if the file has the email addresses one after the new line. Once that is done, i need to get those email addresses in a Array.

Thanks for the suggestion. Please help in the above scenario if you can.

Thanks,
Nirmal
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

Hi Nirmal,

You can remove the new line from the file before you start processing for the email addresses and then you can store the emails in array and validate those email addresses.

Regards
Jatan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading the content of the uploaded file in Struts2
 
Similar Threads
Accesing an unshared folder using java
Redirect to a file on server
how to read a file data into byte array
java.io.FileNotFoundException
checking content type of file upload?