File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes renaming an uploaded file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "renaming an uploaded file" Watch "renaming an uploaded file" New topic

renaming an uploaded file

John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230

I am having no success renaming a file uploaded from a servlet.

always returns false. Note that the File object to be renamed has just been uploaded and not persisted anywhere. I want to save it to the filesystem after I rename it. Anybody have a suggestion?

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

So, your servlet uploaded a file and stored it on your server's file system? And now it tries to rename the file on your server's file system and it fails? No... you said the file hadn't been persisted anywhere, so probably that's not it.

Or were you expecting to rename a file on the client? You certainly can't do that from the server.

I think your question needs a little more clarification, if those guesses didn't answer it.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

Originally posted by John Eric Hamacher:
I want to save it to the filesystem after I rename it.

Paul's on it. If the file hasn't been persisted, where did the File instance come from? And what are you renaming it from?

[Asking smart questions] [About Bear] [Books by Bear]
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
Thanks . . .

I have uploaded the file using a Struts 2 action. The framework automatically places a reference to the uploaded in the action property whose name matches <s:file name="image" label="Picture"/>, so I have:

private File image = null;

in my action with the appropriate getter and setter, of course.

I have not called createNewFile() or saved the object to any filesystem (Whether the file was saved in a temporary directory by the servlet container I don't know). It would just be as if I simply created a File object using:

and immediately wanted to rename it on the next line. Now calling

and then calling

returns "oldfilename.png"

Mind you that new File("newfilename.png") does not exist as an actual file, it just is the name I want associated with my File object.

No other processes have a handle on the file being uploaded and I have the rights to rename the file (but that shouldn't matter anyway since the file is in memory).

If I am misled anywhere, please let me know.

Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
Completely relying on File.renameTo is not a good idea if you ask to me, as an example remember that it always fails (hence returning false) if you're trying to move the file from a physical drive to another one, no matter if you got the rights to do it on both the drives.
On our production servlet we went with something like this :

Where physicalCopy is something like

This way, you can get profit from the faster renameTo operation, and if it fails somehow you can still get your file renamed wherever you want (as long as you've the rights to do it).
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
Thank you, Matteo. I ended up doing a similar thing by using the Commons IO FileUtils.copyFile(File, File).

I agree. Here's the link:
subject: renaming an uploaded file
It's not a secret anymore!