• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Path on servlet

 
Ranch Hand
Posts: 77
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Nandita Tiwari
Ranch Hand
Posts: 77
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Nandita Tiwari
Ranch Hand
Posts: 77
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Nandita Tiwari
Ranch Hand
Posts: 77
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, then how am I suppose to obtain and use these contents?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the ServletsFaq for information on file uploading.
 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic