• 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

renaming files after uploading

 
Ranch Hand
Posts: 83
MS IE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
J Das
Ranch Hand
Posts: 83
MS IE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
J Das
Ranch Hand
Posts: 83
MS IE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ??
 
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
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ''".
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic