0

I want to version the file name with incremental number if the file exists which doesn't delete my old file. Please help me. Thanks!

List items = upload.parseRequest(request);
FileItem file = (FileItem)items.get(1);
String fileName = file.getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
if(storeFile.exists()){
    **/*Do logic to change the filename of the existed file*/**
}else{
   System.out.println(filePath);
   file.write(storeFile);
}


@BalusC this what I do sir:

String prefix = FilenameUtils.getBaseName(fileName); 
String suffix = FilenameUtils.getExtension(fileName);
File newfileName = File.createTempFile(prefix + "-", "." + suffix, new File(uploadPath));
file.write(newfileName);

My first upload is ok with result of: Programming Language Reflection.docx But my second upload resulted to: Programming Language Reflection15897278330376654158

What is the cause of this?

Jesse Abucejo
  • 67
  • 2
  • 11
  • A pretty basic and probably naive implementation would be renaming the new file with the last version of it (2, 3, 4, and on) and have another file (or table in database) where you have the latest version for the file. Still, instead of reinventing the wheel, I would recommend using a [Content Management System](http://en.wikipedia.org/wiki/Content_management_system) to handle the file versioning and all that hard and nasty work instead. – Luiggi Mendoza Jul 02 '13 at 15:41
  • But sir isn't possible if i can add a character or date or incremental number to the prefix of the file to make it unique? I want implement file versioning if the file exists. As your saying, is there an api of Content Management System in java? – Jesse Abucejo Jul 02 '13 at 15:54
  • Access to the wikipedia link I've provided in my comment (it's not to make my comment good looking), you will find a [list of content management system frameworks](http://en.wikipedia.org/wiki/List_of_content_management_frameworks), and some of them in Java. I've worked with a CMS in the past to handle this work, but it will depend on your needs to find the best suited for you. – Luiggi Mendoza Jul 02 '13 at 15:56

1 Answers1

-2

Not exactly what you asked for, but File.createTempFile might do the trick. It inserts a random number to the temp file name, instead of an incremental number.

File tempFile = File.createTempFile(fileName, null, new File(uploadPath));
lreeder
  • 12,047
  • 2
  • 56
  • 65
  • I wouldn't opt for this since looks like OP's working on a web application and two or more users can load the same file at the time. So, to avoid concurrent operations, it would be better using a specialized system like a Content Management System that already handles all this nasty work. – Luiggi Mendoza Jul 02 '13 at 15:45
  • @Luiggi Accoding to the javadoc, this should be OK for concurrent ops: "it is guaranteed that: The file denoted by the returned abstract pathname did not exist before this method was invoked". It's not obvious from the java doc how atomic this operation is, but given the fact that random numbers are used, if it's not atomic it's probably safe enough for all but the most loaded webapps. – lreeder Jul 02 '13 at 15:51
  • Ok, now how will you know which file is **the latest version**? Again, all this is handled cleanly using a specialized system. – Luiggi Mendoza Jul 02 '13 at 15:52
  • @Luiggi The OP didn't indicate a need to version the files, he or she just didn't want to clobber the other file version. A CMS solution might be good for some complex applications, but overkill for many others. – lreeder Jul 02 '13 at 16:09
  • @Ireeder sir how can I convert the .tmp file to its original file extension? – Jesse Abucejo Jul 02 '13 at 16:14
  • Now the problem for concurrent uploads would be which file to choose for downloading it. – Luiggi Mendoza Jul 02 '13 at 16:16
  • sir @Luiggi i've search to the frameworks that support the content management system. I'm using apache tomcat. The list of content management frameworks in the wiki doesn't include apache tomcat. – Jesse Abucejo Jul 02 '13 at 16:20
  • @JesseAbucejo the frameworks will work as third party libraries, note that Tomcat is just the application server that will run your code and the third party libraries code. It would be better to learn more about Java development and server side development first. – Luiggi Mendoza Jul 02 '13 at 16:25
  • @Jesse The createTempFile method allows you to pass in an extension in the second argument. If you provide null instead of the extension, the method uses "tmp" as a default extension. If you need to maintain the original extension, you'll need to extract it from the original file name and pass it to createTempFile. There are some good approaches for doing that in this SO answer: http://stackoverflow.com/questions/3571223/how-do-i-get-the-file-extension-of-a-file-in-java – lreeder Jul 02 '13 at 17:07
  • 1
    I got the extension by this snippet using Guava library: 'String extension = Files.getFileExtension(fileName);' and pass it on the createTempFile: 'File newFile = File.createTempFile(fileName, extension, new File(uploadPath));' but I get bubblesortjavascript.txt8629429337542875422txt. Why? and i don't get the correct file upload sir. – Jesse Abucejo Jul 02 '13 at 19:46
  • @JesseAbucejo if this answer doesn't solve the problem, please unmark it as the accepted answer. – Luiggi Mendoza Jul 02 '13 at 21:20
  • @JesseAbucejo The createTempFile algorithm takes the fileName ( bubblesortjavascript.txt), appends a random Long to it (8629429337542875422), and then the file extension ("txt"). That's how you arrive at that long name. – lreeder Jul 03 '13 at 02:34
  • `createTempFile` prefix must be sole filename like so `bubblesortjavascript` and the suffix must be the file extension like so `.txt`. You apparently did it wrong. You can use `FilenameUtils` to extract those values. See also http://stackoverflow.com/questions/14211843/how-to-save-uploaded-file/14214223#14214223 – BalusC Jul 03 '13 at 13:01