92 lines
2.8 KiB
Java
92 lines
2.8 KiB
Java
package com.pms.common;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import com.oreilly.servlet.multipart.FileRenamePolicy;
|
|
|
|
public class FileRenameClass implements FileRenamePolicy {
|
|
|
|
//public static Map<String, Object> fileMap = new HashMap<String, Object>();
|
|
//public static List<Map<String, Object>> fileList = new ArrayList();
|
|
public List<Map<String, Object>> fileList = new ArrayList();
|
|
/*
|
|
private String time; // point 14 'yyyyMMddHHmmss'
|
|
private String userId;
|
|
public FileRenameClass( String userId , String time){
|
|
this.userId = userId;
|
|
this.time = time;
|
|
}
|
|
*/
|
|
public FileRenameClass() {}
|
|
|
|
|
|
//public static List<Map<String, Object>> getFileList(){
|
|
public List<Map<String, Object>> getFileList(){
|
|
return fileList;
|
|
}
|
|
|
|
//public static void clear(){
|
|
public void clear(){
|
|
fileList = new ArrayList();
|
|
}
|
|
|
|
public File rename(File f){
|
|
// Get the parent directory path as in h:/home/user or /home/user
|
|
String parentDir = f.getParent();
|
|
|
|
// Get filename without its path location, such as 'index.txt'
|
|
String fname = f.getName();
|
|
String fileExt = "";
|
|
|
|
int i = -1;
|
|
if ((i = fname.lastIndexOf(".")) != -1) {
|
|
fileExt = fname.substring(i); // only file extension
|
|
fname = fname.substring(0, i); // except file extension
|
|
}
|
|
|
|
|
|
int counter = 0;
|
|
boolean fExist = true;
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
String fullFileName = parentDir + "\\" + fname.replaceAll(" ", "") + "_" +currentTime+""+ fileExt;
|
|
String savedFileName = fname.replaceAll(" ", "") + "_" +currentTime+""+ fileExt;
|
|
|
|
while (fExist) {
|
|
if (new File(fullFileName).exists()) {
|
|
counter = counter + 1;
|
|
// fullFileName = parentDir + "\\" + fname + "(" + counter + ")"+ fileExt;
|
|
fullFileName = parentDir + "\\" + fname.replaceAll(" ", "") + "_" +currentTime+""+ fileExt;
|
|
savedFileName = fname.replaceAll(" ", "") + "_" +currentTime+""+ fileExt;
|
|
} else {
|
|
fExist = false;
|
|
}
|
|
}
|
|
Map fileMap = new HashMap();
|
|
fileMap.put("realFileName", fname+fileExt);
|
|
fileMap.put("savedFileName", savedFileName);
|
|
fileMap.put("savedFullFileName", fullFileName);
|
|
fileMap.put("fileExt", fileExt.substring(fileExt.indexOf(".")+1));
|
|
fileMap.put("fileSize", f.length());
|
|
fileMap.put("filePath", parentDir);
|
|
|
|
System.out.println("--------------------------------");
|
|
System.out.println("fileName : "+fname);
|
|
System.out.println("fileExt : "+fileExt);
|
|
System.out.println("fullFileName : " + fullFileName);
|
|
System.out.println("savedFileName : "+savedFileName);
|
|
System.out.println("fileSize : "+f.length());
|
|
System.out.println("filePath : "+parentDir);
|
|
System.out.println("--------------------------------");
|
|
|
|
File file = new File(fullFileName);
|
|
fileList.add(fileMap);
|
|
return file;
|
|
}
|
|
}
|