Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
a kishore
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9520
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9520
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic