package com.hlj.util;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class UploadImage {
public static String uploadImage(File file,String fileName,String oldFilePath){
//根据图片地址查找原图片并删除
if(oldFilePath!=null){
File oldFile = new File(ServletActionContext.getServletContext().getRealPath("/frontJsp/wisEstateFront")+"\\"+oldFilePath);
if(oldFile.exists()){
oldFile.delete();
}
}
//文件名为日期格式
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//上传文件夹每天产生一个
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
Date curDate = new Date(System.currentTimeMillis());
String now = sf.format(curDate);
String dayPath = sf1.format(curDate);
String path = ServletActionContext.getServletContext().getRealPath("/frontJsp/wisEstateFront/uploads")+"\\"+dayPath;
File realpath = new File(path);
if(!realpath.exists()){
realpath.mkdirs();
}
String newFileName = now+fileName.substring(fileName.lastIndexOf("."));
File savefile = new File(realpath, newFileName);
try {
FileUtils.copyFile(file, savefile);
} catch (IOException e) {
e.printStackTrace();
}
String image_path = path.substring(path.lastIndexOf("uploads"))+"\\"+newFileName;
//返回处理之后的图片地址
return image_path;
}
public static String uploadImageByPath(File file,String fileName,String oldFilePath,String where){
//根据图片地址查找原图片并删除
if(oldFilePath!=null){
File oldFile = new File(ServletActionContext.getServletContext().getRealPath(where)+"/"+oldFilePath);
if(oldFile.exists()){
oldFile.delete();
}
}
//文件名为日期格式
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//上传文件夹每天产生一个
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
Date curDate = new Date(System.currentTimeMillis());
String now = sf.format(curDate);
String dayPath = sf1.format(curDate);
String path = ServletActionContext.getServletContext().getRealPath(where+"/uploads")+"/"+dayPath;
File realpath = new File(path);
if(!realpath.exists()){
realpath.mkdirs();
}
String newFileName = now+fileName.substring(fileName.lastIndexOf("."));
File savefile = new File(realpath, newFileName);
try {
FileUtils.copyFile(file, savefile);
} catch (IOException e) {
e.printStackTrace();
}
String image_path = path.substring(path.lastIndexOf("uploads"))+"/"+newFileName;
//返回处理之后的图片地址
return image_path;
}
}