• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

renaming an uploaded file

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello:

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?

Thanks
 
Sheriff
Posts: 28394
100
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
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.
 
Sheriff
Posts: 67754
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

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?
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Eric
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Matteo. I ended up doing a similar thing by using the Commons IO FileUtils.copyFile(File, File).

eric
reply
    Bookmark Topic Watch Topic
  • New Topic