• 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

Getting absolute path from file uploaded

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to get the absolute file path when uploading a file using html:file in Struts 1.3. In the Action Controller, I have some methods available from the file (like getFileName() or getContentType) but none of them gives me the path I need. Is there anything I could do?

Nevermind, I could do it using java.io.File. Thanks!
 
Nicolás Muñoz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found another problem. This is perhaps less related to Struts and more to Tomcat, but as I started this thread, I thought I should post it here. When I ask for the Absolute Path of my file filename, it returns

This is not where the real file is. I imagine that, as Tomcat is the web server, then it manages the files as it wants. But I still can't get the REAL path of my file. Any help?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a file which is uploaded from the client where your HTML is displayed? Then you can't get the absolute path which was its location on the client, no. But that really doesn't matter because there isn't anything useful you could do with that information anyway. Not to mention that most browsers won't tell you the path because it could violate the privacy of the client and perhaps even provide information to malware trying to break into the client's system.

And if you successfully used a File object on an uploaded file, that suggests that your client and your server are the same machine. In real life they will be different machines and that won't work.
 
Nicolás Muñoz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, yes, the client and the server are being used in the same machine and for this particular application, it will be like this always. I needed the direction of the file to copy that file in another location. When I try to do this, it's not successful so I was thinking that maybe the problem was that the File didn't have it's real direction in getAbsolutePath().

Anyway, I've been trying all of this because I need to let the user (which will use the system in the same machine) upload a file (an image file in this case) which should be stored in a MySQL database using Hibernate, giving me the chance to later retrieve this image and display it. Trouble is, I read that it was discouraged uploading files to a database and that it was better to just have stored the path of the file in the database (hence, that's why I needed the path).

I've successfully added image files as byte[] (BLOB) to the database, but getting them later has been difficult for me. I understand what you said about security, about not giving the right to look at people's path, but what surprises me is that even when I try to make a copy to the file, which supposedly temporarily lies in apache's bin folder, it doesn't work right (it doesn't copy the file). This is a sample from my code:



actualPath has the apache's bin directory, not the path where my Action Controller actually lies. But still, f.renameTo () should make a copy of File f in actualPath, and it does not do it.
Any thoughts?
 
reply
    Bookmark Topic Watch Topic
  • New Topic