Spring Java ajaxfileupload.js 上传头像 3.0

来源:互联网 发布:mac桌面出现ds store 编辑:程序博客网 时间:2024/05/16 08:33

新需求:

需要把上传的一张图片自动压缩成几张不同大小的图片

开始比较有疑惑,我要压缩的肯定是大小就是KB这种的,但代码看起来是压缩的宽高而已啊,其实都压缩了,试试就知道了

之前的两篇:

一,Spring Java ajaxfileupload.js 上传头像 - qq_24435837的博客 - 博客频道 - CSDN.NET
http://blog.csdn.net/qq_24435837/article/details/52460855

二,Spring Java ajaxfileupload.js 上传头像 2.0 - qq_24435837的博客 - 博客频道 - CSDN.NET
http://blog.csdn.net/qq_24435837/article/details/52729763

上传的java代码:

<span style="white-space:pre"></span>@ResponseBody@RequestMapping(value = "uploadImage", method = RequestMethod.POST)    public String uploadImage(@RequestParam MultipartFile[] myfiles, HttpServletRequest request, HttpServletResponse response) throws IOException{  String memberId = CookieHelper.getByName(App.COOKIE_USER_ID);——一个ID对应一个头像String realPath = ImageHelper.headResolve();——写在配置文件里的文件夹路径(直接写死的)        File file = new File(realPath);            if (!file.getParentFile().exists()) {        file.getParentFile().mkdirs();        }——没有该文件夹则创建        String filePath = null;                for(MultipartFile myfile : myfiles){                     if(!myfile.isEmpty()){                                if(myfile.getSize() <= 1*1024*1024){                String originalFilename = myfile.getOriginalFilename();                    String FileFormat = originalFilename.substring(originalFilename.lastIndexOf(".")+1);                    String realFileName = memberId +"."+ FileFormat;——把id作为新的图片名,不用删除之前的,因为同名会直接覆盖                    String FilePathL =  realPath+memberId+"_" + 160+"_"+160+"."+FileFormat;——某大小的图片名                    String FilePathM = realPath+memberId+"_" + 100+"_"+100+"."+FileFormat;                    String FilePathS = realPath+memberId+"_" + 60+"_"+60+"."+FileFormat;                                            try {                    FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(file, realFileName));——上传                     ImgCompress imgCom = new ImgCompress(realPath + realFileName); ——调用函数压缩                    imgCom.resize(160, 160,FilePathL,FileFormat);                    imgCom.resize(100, 100,FilePathM,FileFormat);                              imgCom.resize(60, 60,FilePathS,FileFormat); </span>                                    memberService.uploadImage(memberId, "head/"+realFileName);——存数据库                    filePath =  "/pic/"+"head/"+realFileName;——这个是自己配的映射路径,这个自己看着办吧,可参考1.0                    memberService.log("系统设置", "基本信息_上传头像");                                    } catch (IOException e) {                       e.printStackTrace();                                    }            }                      }                     }         return filePath;    }  
压缩的java代码:
package com.bf.health.util;import java.io.*;  import java.awt.*;  import java.awt.image.*;  import javax.imageio.ImageIO;public class ImgCompress {      private Image img;      private int width;      private int height;      /**      * 构造函数      */      public ImgCompress(String fileName) throws IOException {          File file = new File(fileName);// 读入文件          img = ImageIO.read(file);      // 构造Image对象          width = img.getWidth(null);    // 得到源图宽          height = img.getHeight(null);  // 得到源图长  }    /**      * 强制压缩/放大图片到固定的大小      * @param w int 新宽度      * @param h int 新高度      */      public void resize(int w, int h,String path,String Filename) throws IOException {          // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢          BufferedImage image = new BufferedImage(w, h,BufferedImage.SCALE_SMOOTH);           image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图          File destFile = new File(path);          FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流          ImageIO.write(image, Filename, new File(path));        out.close();      }  }  
参考博文:

一,Java中图片压缩处理 - java小强 - ITeye技术网站
http://cuisuqiang.iteye.com/blog/2045855

二,关于JPEGImageEncoder的问题-CSDN论坛-CSDN.NET-中国最大的IT技术社区
http://bbs.csdn.net/topics/310151079

三,关于JPEGImageEncoder的问题-CSDN论坛-CSDN.NET-中国最大的IT技术社区
https://my.oschina.net/JustLoveIT/blog/474004

0 0