Spring Java ajaxfileupload.js 上传头像 3.0

新需求:

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

开始比较有疑惑,我要压缩的肯定是大小就是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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值