public static String fileUpLoad(MultipartFile file,String url){
if(file!=null&&!file.getOriginalFilename().equals("")){
Calendar date = Calendar.getInstance();
int day = date.get(Calendar.DAY_OF_MONTH);
int month = date.get(date.MONTH);
int year = date.get(date.YEAR);
long timeStamp = CalendarUtils.getTimeStamp();
String str = year+""+month+"/"+day;
File file1 = new File(SystemConstants.BASE_URL+str+"/"+url);
if(!file1.exists()){
file1.mkdirs();
}
String name = file.getOriginalFilename();
String[] split = name.split("\\.");
String string = split[split.length-1];
String fileName = timeStamp+"."+string;
File file2 = new File(file1,fileName);
if(!file2.exists()){
try {
file.transferTo(file2);
} catch (Exception e) {
e.printStackTrace();
}
}
return SystemConstants.REDIRECT_URL+str+"/"+url+fileName;
}else{
return null;
}
}
/**
* 更新文件
* @param file
* @param oldUrl
* @param newUrl
*/
public static String fileUpdate(MultipartFile file,String oldUrl,String newUrl){
if(file!=null){
if(oldUrl!=null&&!oldUrl.equals("")){
String deleteUrl = oldUrl.replace(SystemConstants.REDIRECT_URL,"");
File file1 = new File(SystemConstants.BASE_URL+deleteUrl);
//System.out.println(SystemConstants.BASE_URL+deleteUrl);
file1.delete();
/*try {
Process process = Runtime.getRuntime().exec("rm -f "+deleteUrl);
System.out.println("删除成功");
} catch (IOException e) {
System.out.println("删除文件失败");
e.printStackTrace();
}*/
}
return fileUpLoad(file, newUrl);
}else{
return null;
}
}
/**
* 删除文件
* @param request
* @param url
*/
public static void deleteOnExit(String url){
if(url!=null){
String deleteUrl = url.replace(SystemConstants.REDIRECT_URL,"");
File file1 = new File(SystemConstants.BASE_URL+deleteUrl);
//System.out.println(SystemConstants.BASE_URL+deleteUrl);
file1.delete();
/*try {
Process process = Runtime.getRuntime().exec("rm -f "+deleteUrl);
System.out.println("删除成功");
} catch (IOException e) {
System.out.println("删除文件失败");
e.printStackTrace();
}*/
}
}
if(file!=null&&!file.getOriginalFilename().equals("")){
Calendar date = Calendar.getInstance();
int day = date.get(Calendar.DAY_OF_MONTH);
int month = date.get(date.MONTH);
int year = date.get(date.YEAR);
long timeStamp = CalendarUtils.getTimeStamp();
String str = year+""+month+"/"+day;
File file1 = new File(SystemConstants.BASE_URL+str+"/"+url);
if(!file1.exists()){
file1.mkdirs();
}
String name = file.getOriginalFilename();
String[] split = name.split("\\.");
String string = split[split.length-1];
String fileName = timeStamp+"."+string;
File file2 = new File(file1,fileName);
if(!file2.exists()){
try {
file.transferTo(file2);
} catch (Exception e) {
e.printStackTrace();
}
}
return SystemConstants.REDIRECT_URL+str+"/"+url+fileName;
}else{
return null;
}
}
/**
* 更新文件
* @param file
* @param oldUrl
* @param newUrl
*/
public static String fileUpdate(MultipartFile file,String oldUrl,String newUrl){
if(file!=null){
if(oldUrl!=null&&!oldUrl.equals("")){
String deleteUrl = oldUrl.replace(SystemConstants.REDIRECT_URL,"");
File file1 = new File(SystemConstants.BASE_URL+deleteUrl);
//System.out.println(SystemConstants.BASE_URL+deleteUrl);
file1.delete();
/*try {
Process process = Runtime.getRuntime().exec("rm -f "+deleteUrl);
System.out.println("删除成功");
} catch (IOException e) {
System.out.println("删除文件失败");
e.printStackTrace();
}*/
}
return fileUpLoad(file, newUrl);
}else{
return null;
}
}
/**
* 删除文件
* @param request
* @param url
*/
public static void deleteOnExit(String url){
if(url!=null){
String deleteUrl = url.replace(SystemConstants.REDIRECT_URL,"");
File file1 = new File(SystemConstants.BASE_URL+deleteUrl);
//System.out.println(SystemConstants.BASE_URL+deleteUrl);
file1.delete();
/*try {
Process process = Runtime.getRuntime().exec("rm -f "+deleteUrl);
System.out.println("删除成功");
} catch (IOException e) {
System.out.println("删除文件失败");
e.printStackTrace();
}*/
}
}