• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Path on servlet

 
Noopur Kore
Ranch Hand
Posts: 77
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have given <input type="file"> tag in my .jsp page. I want to send the filepath to my servlet. Any idea how it is done?
I'm getting the filepath as ---> C:\fakepath\filename

Please help guys.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot get the file path reliably. Some buggy versions of IE may send it, but most browsers will not. If your design relies upon knowing the file path, you'll need to revisit those designs.
 
Noopur Kore
Ranch Hand
Posts: 77
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea on what changes should I make? I want to upload the file on my servlet, work on it and display it on another JSP.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Noopur Kore ,

I had faced a similar scenario to yours a while back. First let me explain what happens to a file when you upload it to the server, it gets stored in a temporary folder where it is kept for access just at runtime only. The only issue is that it is a temp folder. When your file uploader thread is done with the uploading , if you do not save the file of reference it from another thread, it will most probably get deleted or stored in some temp cache folder that you wont be able to locate at all ( atleast this is what happened with me but I was using JBOSS server ). Now the temp folder of tomcat is the work directory located at $CATALINA_HOME$/work and the complete location to the work directory specific to your project will be $CATALINA_HOME$/work/Catalina/localhost/<project_context_name>/ .

That said , lets come to the upload part , when you upload a file via form , it will be present in this work directory . Now when you upload it , it will appear in the work directory but it will be deleted after the upload thread gets done with it , so what you will do is that you will save it. Once saved, you can do whatever you like with it .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, depending upon how you configure the file upload library, the file can be stored anywhere; not just in the work folder.

The point is, where the original file came from on the client machine is not available in most browsers.
 
Noopur Kore
Ranch Hand
Posts: 77
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea on how am I suppose to upload the file? I think the option of obtaining the file path is closed then, right?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to be under the impression that your servlet has to know the path on the client computer file system.

Nope, the client browser handles that path information to open that file and send the contents in a request.

What a security hazard it would be if your server could address files on the client directly!

Bill
 
Noopur Kore
Ranch Hand
Posts: 77
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, then how am I suppose to obtain and use these contents?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the ServletsFaq for information on file uploading.
 
K. Gil
Ranch Hand
Posts: 75
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a security hazard it would be if your server could address files on the client directly!


it was always like that with M$ IE

Event 1056 - File Name Restriction
http://msdn.microsoft.com/en-gb/library/dd565634(v=vs.85).aspx
Build date: 11/6/2012

 
Tim Holloway
Saloon Keeper
Pie
Posts: 18282
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MS IE, security hazard, need we say more? Anyway the link is broken.

In any event, the file upload in HTTP doesn't allow the server to open the client file. Instead the HTTP client opens the client file, copies its contents to the HTTP MIME data stream, and the server caches the bytes until the server application figures out what it wants to do with the data. Which may or may not involve saving that data (verbatim or modified) to a server-side file at the whim of the application programmer.

The "file name" is simply another data item in the uploaded MIME data stream, and can actually be any string that the client application sets it to. It's simply that the accepted/expected value of that string is the name of the file.
 
Tiklu Mukherjee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noopur Kore wrote:Any idea on what changes should I make? I want to upload the file on my servlet, work on it and display it on another JSP.



Hi Noopur,

You can use a Similar logic as below ,by using: DiskFileItemFactory


or else you can check out this link it might help you.

http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic