上传图片,防止忘记,随时可以看!!
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
req.getSession().getServletContext());
// 检查form是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(req)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) req;
Iterator<String> iter = multiRequest.getFileNames();
if (iter.hasNext()) {
// 由CommonsMultipartFile继承而来,拥有上面的方法.
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {
//request.getContextPath()返回当前页面所在的应用的名字
String path = req.getContextPath();
//request.getScheme()返回当前请求所使用的协议;request.getServerName()返回当前页面所在的服务器的名字
String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"";
//String portraitPath = basePath+"/"+user.getPortraitPath(); //获取用户头像地址
String filenameOld = file.getOriginalFilename();
String extName = filenameOld.substring(filenameOld.lastIndexOf("."));// 扩展名
String filename = StringHelper.randomString(10, StringHelper.SEED_LANDL) + extName;//上传后文件名规则: 10为随机数+原文件后缀
String savePath2 = req.getSession().getServletContext().getRealPath("");
savePath2 = savePath2.replaceAll("\\\\","/");
//String savePath = req.getSession().getServletContext().getRealPath("/portrait/temporary/"); //文件夹
String savePath = StaticProperties.getProperty("temporaryPath"); //获取临时文件夹目录
String webPath = StaticProperties.getProperty("webPath");
String path1 = savePath.substring(savePath.indexOf(webPath), savePath.length())+filename; //截取网络访问地址用于预览
String path2 = savePath.substring(savePath.indexOf(webPath), savePath.length())+"";
String save =savePath2+"/"+path2;
String save1 =save+""+filename;
File localFile = new File(save, filename);
if (!localFile.getParentFile().exists()) {
localFile.getParentFile().mkdirs();
}
file.transferTo(localFile); //保存原头像
reduce(savePath,filename, 300, 350,true); //压缩头像
resp.getWriter().write("{\"uploadMsg\": \"success\", \"portraitName\": \"" +filename + "\", \"portraitPath\": \"" +path1+ "\"}");
return null;
}
}
}
} catch (Exception e) {
e.printStackTrace();
try {
resp.getWriter().write("{\"uploadMsg\": \"failure\"}");
} catch (IOException e1) {
e1.printStackTrace();
}
}
return null;
@param base 根目录* @param imgPath 要压缩的图片路径
* @param width 压缩宽
* @param height 压缩高
* @param percentage 是否等比例压缩,true则宽高比自动调整
File srcfile = new File(base+"/"+imgPath);
BufferedImage src = ImageIO.read(srcfile);
if (percentage) {
double rate1 = ((double) src.getWidth(null)) / (double) width + 0.1;
double rate2 = ((double) src.getHeight(null)) / (double) height + 0.1;
double rate = rate1 > rate2 ? rate1 : rate2;
width = (int) (((double) src.getWidth(null)) / rate);
height = (int) (((double) src.getHeight(null)) / rate);
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(src.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), 0, 0, null);
FileOutputStream out = new FileOutputStream(base+"/"+imgPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
image.flush();
out.flush();
out.close();
src.flush();