File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Getting absolute path from file uploaded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Getting absolute path from file uploaded" Watch "Getting absolute path from file uploaded" New topic

Getting absolute path from file uploaded

Nicolás Muñoz

Joined: Nov 11, 2009
Posts: 17
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 Thanks!
Nicolás Muñoz

Joined: Nov 11, 2009
Posts: 17
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?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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

Joined: Nov 11, 2009
Posts: 17
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?
I agree. Here's the link:
subject: Getting absolute path from file uploaded
It's not a secret anymore!