Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Reading the content of the uploaded file in Struts2

 
Greenhorn
Posts: 5
Oracle Spring Java
  • 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
 
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!
 
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.
 
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

 
Ranch Hand
Posts: 300
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: 300
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
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic