Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes renaming files after uploading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "renaming files after uploading" Watch "renaming files after uploading" New topic
Author

renaming files after uploading

J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Hi to all . . .. I am developing a web application using JSP, servlet where users can upload files to their accounts and also download files .. The uploaded files gets saved in a folder (that gets created as soon as an user registers himself with the site .. ).. Now when the user logs in and uploads file , the files are uploaded in that folder in the hard disk.. I have saved only the path and filename in MySql database and not the actual file.. now if i want to rename the uploaded files , what should i do ?? I mean i can rename the filename saved in database but i am not able to rename the actual file in the uploaded folder .. Please help..
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Why would you change the file name in database if their names are not changed on harddisk? If you still want to rename files, you can write shell scripts for the same.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16093
    
  21

Although you specifically mentioned that you're uploading files to a folder, I'll go ahead with the standard warning that you should never upload anything to be inside your WAR or appserver directories, just in case you thought that you could create upload folders in the WAR (Well, you often can, but sooner or later, you'll regret it).

And, of course, uploading actually gets staged from a general upload facility, so that the incoming data has to be serialized out to a file that you yourself create - you shouldn't expect that data to live indefinitely in the staging area.

Once the preceding annoying details have been attended to, the actual process of renaming a file is fairly easy. Just use the java.io.File renameTo() method.


Customer surveys are for companies who didn't pay proper attention to begin with.
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Tim Holloway wrote:Although you specifically mentioned that you're uploading files to a folder, I'll go ahead with the standard warning that you should never upload anything to be inside your WAR or appserver directories, just in case you thought that you could create upload folders in the WAR (Well, you often can, but sooner or later, you'll regret it).

And, of course, uploading actually gets staged from a general upload facility, so that the incoming data has to be serialized out to a file that you yourself create - you shouldn't expect that data to live indefinitely in the staging area.

Once the preceding annoying details have been attended to, the actual process of renaming a file is fairly easy. Just use the java.io.File renameTo() method.



I am creating the folder outside my web application ( somewhere on my local disk ) .. are you warning me because of re-building my app might erase my uploaded files and folders if i upload inside the web app ?? Is this the reason or anything else ? and moreover , the java.io.File renameTo() method is not helping me in achieving what i want . . Please help..
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16093
    
  21

Having your data get nuked is one very good reason for not keeping it in the WAR, yes. The other is that according to the J(2)EE standards, the WAR is a ZIP file, and therefore its insides are not accessible via ordinary filesystem read, write, and directory operations. In the Real World, a lot of appservers will unpack ("explode") WARs, but it's not guaranteed, and it's often an option that the sysadmin can disable.

There a a number of reasons why renameTo() can fail. I believe one of them is in cases where you want to move the file to a different drive letter (for that, you have to do things the hard way, via copy-and-delete). So if you could provide some details on how your rename attempt is set up and what error response you get, it would be a big help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Tim Holloway wrote:I believe one of them is in cases where you want to move the file to a different drive letter (for that, you have to do things the hard way, via copy-and-delete).

My experience has shown otherwise (I've even been able to use renameTo with file shares), but it may very from one Windows system to another.

The top 2 causes of renameTo failing are 1) the file still being in use, either by the JVM or by some other program, and 2) the destination "file" already existing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

Tim Holloway wrote:
There a a number of reasons why renameTo() can fail. I believe one of them is in cases where you want to move the file to a different drive letter (for that, you have to do things the hard way, via copy-and-delete). So if you could provide some details on how your rename attempt is set up and what error response you get, it would be a big help.


this is the jsp from where i am renaming that file :



and this is the servlet where the file should get renamed ..



but i am not getting any result.. what should i do to get the file renamed ??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

First of all, don't create file paths by string concatenation. It's easy to miss out a path separator, or to get too many of them, and so on. Here's what you should do instead:



You'll want to confirm that what you get there is a file which actually exists on your system. Calling "renameTo" on a non-existent file doesn't throw any exceptions. It does return true or false to report on what it did, but your code is ignoring that.

You'll also want to confirm that the earlier code which created that file did actually close the output stream, too. We can't tell either of those things from your code.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Off-topic, you shouldn't use Java code (scriptlets) inside JSP anymore. Your code can easily be adapted to use a forEach tag:
You may have to rename your methods to getMyfilename and getFid. However, you should do that anyway. Even moreso, it should be getMyFileName, and the value in the JSTL tags should then become ${b.myFileName}.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16093
    
  21

Continuing:


Note that in a "real" webapp, I'd use something more professional than System.out.println, but this is a quick-and-dirty example. However, if you'll notice, I did format the prints to put the filenames in quotes. Saves me from headscratchers like "Old filename was 'C:\Program Files\Filename with trailing spaces ''".
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

I would suggest a different design altogather, as anyone would be easily able to break your upload code in many ways (discussing that is out of topic). The better design could be like this -

add a column to your database table named as actualName,
whenever there is an upload, save the file in disk named as some GUID, and then make an entry in your database with actualName column as the generated GUID (physical file name).
whenever rename is requested, just change fileName column (not actualName) of your database & never the physical file.
This will improve renaming time as well, also this can save you from basic level hackings & spaces in names/invalid names etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: renaming files after uploading