Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes File Path on servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "File Path on servlet" Watch "File Path on servlet" New topic
Author

File Path on servlet

Noopur Kore
Ranch Hand

Joined: Aug 18, 2012
Posts: 77

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

Joined: Jan 10, 2002
Posts: 61010
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Noopur Kore
Ranch Hand

Joined: Aug 18, 2012
Posts: 77

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

Joined: Aug 11, 2011
Posts: 440

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 .


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

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

Joined: Aug 18, 2012
Posts: 77

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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Aug 18, 2012
Posts: 77

Ok, then how am I suppose to obtain and use these contents?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

See the ServletsFaq for information on file uploading.
K. Gil
Ranch Hand

Joined: Apr 29, 2011
Posts: 75

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

Joined: Jun 25, 2001
Posts: 16014
    
  20

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tiklu Mukherjee
Greenhorn

Joined: Mar 14, 2013
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Path on servlet