• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading the content of the uploaded file in Struts2

 
Damodar Wagle
Greenhorn
Posts: 5
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic