GeeCON Prague 2014*
The moose likes Struts and the fly likes How to get Full path of a file from <html:file> tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to get Full path of a file from <html:file> tag" Watch "How to get Full path of a file from <html:file> tag" New topic
Author

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

a kishore
Ranch Hand

Joined: Sep 16, 2009
Posts: 82
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

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


I love this place!
a kishore
Ranch Hand

Joined: Sep 16, 2009
Posts: 82
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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

Joined: Sep 16, 2009
Posts: 82
Thank you for that idea, im doing that now. once i finish that i will update here.

thank you again.
a kishore
Ranch Hand

Joined: Sep 16, 2009
Posts: 82
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.
 
GeeCON Prague 2014
 
subject: How to get Full path of a file from <html:file> tag