• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to get Full path of a file from <html:file> tag

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Im using <html:file> tag to upload one CSV file and i want to read that in Action Class.
Im trying to read file using FormFile:

FormFile myFile = myForm.getTheFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();

String thisLine; //string variable which take each record at a time
int count=0; //to count no. of records

FileInputStream fis = new FileInputStream(fileName);
DataInputStream myInput = new DataInputStream(fis);

while ((thisLine = myInput.readLine()) != null)
{ //beginning of outer while loop
StringTokenizer st =new StringTokenizer(thisLine,",");
while(st.hasMoreElements()){
String field = st.nextToken();
System.out.print(field+", ");
}
System.out.println();

} //ending of outer while loop


when i run JSP in browser i get an error like this:
java.io.FileNotFoundException: BKHN-290709-050809.csv (The system cannot find the file specified)

I understood that, may be FileInputStream is not getting absolute path of that file. so that is giving that error. bcuz when i hardcode filepath it is working fine.
If that is correct, can anyone tell me how to get absolute path and try to solve this? i need this urgently

thanks alot in advance
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it. Also please Use Real Words on javaranch. Why do you want to read the file again?? You already have the content of the file in fileData. You can use that to do whatever you want...
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

Is your Tomcat instance running on the same PC as the file that you are trying to upload?
It sounds like your application might be reading the file directly from the filesystem rather than through HTTP?

Sean
 
a kishore
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit and Sean for your time.

Ankit,
Im relatively new to struts tags. As you said i got that fileData but im not sure how to extract data using StringTokenizer for a CSV file.
If you have any sample code please post it here.

Sean,
Tomcat is running in my PC only.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A CSV file as I remember is a comma separated file. You can convert the byte array to String (check the String class constructors), split the String on \n and then use that in the StringTokenizer...
 
a kishore
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for that idea, im doing that now. once i finish that i will update here.

thank you again.
 
a kishore
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Ankit,

byte[] fileData = myFile.getFileData();
String strFileContent = new String(fileData);

this code gives array of strings. now im able to split and store into database properly.

thankyou for your valuable suggestions.
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic