1 5 package com.oreilly.servlet.multipart; 6 7 import java.io.*; 8 9 23 public class DefaultFileRenamePolicy implements FileRenamePolicy { 24 25 public File rename(File f) { 28 if (createNewFile(f)) { 29 return f; 30 } 31 String name = f.getName(); 32 String body = null; 33 String ext = null; 34 35 int dot = name.lastIndexOf("."); 36 if (dot != -1) { 37 body = name.substring(0, dot); 38 ext = name.substring(dot); } 40 else { 41 body = name; 42 ext = ""; 43 } 44 45 int count = 0; 51 while (!createNewFile(f) && count < 9999) { 52 count++; 53 String newName = body + count + ext; 54 f = new File(f.getParent(), newName); 55 } 56 57 return f; 58 } 59 60 private boolean createNewFile(File f) { 61 try { 62 return f.createNewFile(); 63 } 64 catch (IOException ignored) { 65 return false; 66 } 67 } 68 } 69 | Popular Tags |