public class UploadFileUtil{
/**
*
* @Title: uploapImage
* @Description: TODO(图像上传)
* @param request http请求
* @param imgPath 图像路径
* @param thumPath 映像图像路径 不生成映像时,此项为空
* @param tWidth 映像宽度
* @param tHeight 映像高度
* @return
* @throws IOException 设定文件
* @return String 返回类型
* @throws
*/
public static String saveLocalImage(HttpServletRequest request,String imgPath,boolean thumFlag,Integer tWidth,Integer tHeight) throws IOException{
// 转型为MultipartHttpRequest:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得文件:
MultipartFile file = multipartRequest.getFile("file");
// 获得文件名:
String filename = file.getOriginalFilename();
//获取后缀名
String suffix = filename.substring(filename.indexOf(".")+1);
String localBasePath = request.getSession().getServletContext().getRealPath("")+imgPath;
Toolkit.buildDirectory(localBasePath+"/image");
Toolkit.buildDirectory(localBasePath+"/thum");
String newFileName = Toolkit.RandomStr()+"."+suffix;
//生成 的本地文件路径、镜像文件以及URI路径
String localFilePath = localBasePath+"/image/IMG_"+newFileName;
// 写入文件
File source = new File(localFilePath);
file.transferTo(source);
//是否生缩略图并返回缩略图路径
if(thumFlag){
String localThumPath = localBasePath+"/thum/TMP_"+newFileName;
//创建缩略图
InputStream is=new FileInputStream(source);
try {
BufferedImage bi=ImageIO.read(is);
//写入缩略图
ImageIO.write(PicZoom.zoom(bi,tWidth,tHeight), suffix, new File(localThumPath));
is.close();
return (imgPath+"/thum/TMP_"+newFileName).replaceAll("//", "/");
} finally{
if(is!=null)is.close();
// TODO: handle exception
}
}
return (imgPath+"/image/IMG_"+newFileName).replaceAll("//", "/");
}
/**
*
* @Title: saveLocalFile
* @Description: TODO(保存本地文件)
* @param request
* @param filePath 文件路径
* @throws IOException 设定文件
* @return void 返回类型
* @throws
*/
public static String saveLocalFile(HttpServletRequest request,String filePath,String fileName) throws IOException{
// 转型为MultipartHttpRequest:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得文件:
MultipartFile file = multipartRequest.getFile("file");
// 获得文件名:
String filename = file.getOriginalFilename();
//获取后缀名
String suffix = filename.substring(filename.indexOf(".")+1);
String localBasePath = request.getSession().getServletContext().getRealPath("")+filePath;
if(Toolkit.isEmpty(fileName)){
fileName = Toolkit.RandomStr()+"."+suffix;
}else{
fileName = fileName +"."+suffix;
}
Toolkit.buildDirectory(localBasePath);
//生成 的本地文件路径、镜像文件以及URI路径
String localFilePath = (localBasePath+"/"+fileName).replaceAll("//","/").replaceAll("\\\\","/");
// 写入文件
File source = new File(localFilePath);
file.transferTo(source);
return (filePath+"/"+fileName).replaceAll("//","/");
}
/**
*
* @Title: uploapImage
* @Description: TODO(图像上传)
* @param request http请求
* @param imgPath 图像路径
* @param thumPath 映像图像路径 不生成映像时,此项为空
* @param tWidth 映像宽度
* @param tHeight 映像高度
* @return
* @throws IOException 设定文件
* @return String 返回类型
* @throws
*/
public static String saveLocalImage(HttpServletRequest request,String imgPath,boolean thumFlag,Integer tWidth,Integer tHeight) throws IOException{
// 转型为MultipartHttpRequest:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得文件:
MultipartFile file = multipartRequest.getFile("file");
// 获得文件名:
String filename = file.getOriginalFilename();
//获取后缀名
String suffix = filename.substring(filename.indexOf(".")+1);
String localBasePath = request.getSession().getServletContext().getRealPath("")+imgPath;
Toolkit.buildDirectory(localBasePath+"/image");
Toolkit.buildDirectory(localBasePath+"/thum");
String newFileName = Toolkit.RandomStr()+"."+suffix;
//生成 的本地文件路径、镜像文件以及URI路径
String localFilePath = localBasePath+"/image/IMG_"+newFileName;
// 写入文件
File source = new File(localFilePath);
file.transferTo(source);
//是否生缩略图并返回缩略图路径
if(thumFlag){
String localThumPath = localBasePath+"/thum/TMP_"+newFileName;
//创建缩略图
InputStream is=new FileInputStream(source);
try {
BufferedImage bi=ImageIO.read(is);
//写入缩略图
ImageIO.write(PicZoom.zoom(bi,tWidth,tHeight), suffix, new File(localThumPath));
is.close();
return (imgPath+"/thum/TMP_"+newFileName).replaceAll("//", "/");
} finally{
if(is!=null)is.close();
// TODO: handle exception
}
}
return (imgPath+"/image/IMG_"+newFileName).replaceAll("//", "/");
}
/**
*
* @Title: saveLocalFile
* @Description: TODO(保存本地文件)
* @param request
* @param filePath 文件路径
* @throws IOException 设定文件
* @return void 返回类型
* @throws
*/
public static String saveLocalFile(HttpServletRequest request,String filePath,String fileName) throws IOException{
// 转型为MultipartHttpRequest:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得文件:
MultipartFile file = multipartRequest.getFile("file");
// 获得文件名:
String filename = file.getOriginalFilename();
//获取后缀名
String suffix = filename.substring(filename.indexOf(".")+1);
String localBasePath = request.getSession().getServletContext().getRealPath("")+filePath;
if(Toolkit.isEmpty(fileName)){
fileName = Toolkit.RandomStr()+"."+suffix;
}else{
fileName = fileName +"."+suffix;
}
Toolkit.buildDirectory(localBasePath);
//生成 的本地文件路径、镜像文件以及URI路径
String localFilePath = (localBasePath+"/"+fileName).replaceAll("//","/").replaceAll("\\\\","/");
// 写入文件
File source = new File(localFilePath);
file.transferTo(source);
return (filePath+"/"+fileName).replaceAll("//","/");
}