package cn.itcast.sh08.struts2.utils;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
public class UploadUtils {
public static String saveUploadFile(File upload){
SimpleDateFormat sdf=new SimpleDateFormat("\\yyyy\\MM\\dd\\");
String basePath=ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
Date date=new Date();
String subPath=sdf.format(date);
File dir=new File(basePath+subPath);
if(!dir.exists()){
dir.mkdirs();
}
String suffix=upload.getName().substring(upload.getName().lastIndexOf("."));
System.out.println(upload.getName());
System.out.println(suffix);
String path=basePath+subPath+UUID.randomUUID().toString()+suffix;
System.out.println(path);
File dest=new File(path);
upload.renameTo(dest);
return path;
}
public static String saveUploadFile(File upload,String fileName){
SimpleDateFormat sdf=new SimpleDateFormat("\\yyyy\\MM\\dd\\");
String basePath=ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
Date date=new Date();
String subPath=sdf.format(date);
File dir=new File(basePath+subPath);
if(!dir.exists()){
dir.mkdirs();
}
String ext=fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println(ext);
String path=basePath+subPath+UUID.randomUUID().toString()+"."+ext;
System.out.println(path);
File dest=new File(path);
upload.renameTo(dest);
return path;
}
}
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
public class UploadUtils {
public static String saveUploadFile(File upload){
SimpleDateFormat sdf=new SimpleDateFormat("\\yyyy\\MM\\dd\\");
String basePath=ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
Date date=new Date();
String subPath=sdf.format(date);
File dir=new File(basePath+subPath);
if(!dir.exists()){
dir.mkdirs();
}
String suffix=upload.getName().substring(upload.getName().lastIndexOf("."));
System.out.println(upload.getName());
System.out.println(suffix);
String path=basePath+subPath+UUID.randomUUID().toString()+suffix;
System.out.println(path);
File dest=new File(path);
upload.renameTo(dest);
return path;
}
public static String saveUploadFile(File upload,String fileName){
SimpleDateFormat sdf=new SimpleDateFormat("\\yyyy\\MM\\dd\\");
String basePath=ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
Date date=new Date();
String subPath=sdf.format(date);
File dir=new File(basePath+subPath);
if(!dir.exists()){
dir.mkdirs();
}
String ext=fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println(ext);
String path=basePath+subPath+UUID.randomUUID().toString()+"."+ext;
System.out.println(path);
File dest=new File(path);
upload.renameTo(dest);
return path;
}
}