java图片压缩上传
java
/**Java 2009-10-27 15:54:14 阅读159 评论0 字号:大中小
package com.xkshow.file;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.io.*;
public class ImgPress {
private static final Log logger = LogFactory.getLog(ImgPress.class);
public ImgPress() {
}
public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist, int benchmark) {
//imgsrc:本地图片路径;imgdist:上传服务器路径;widthdist:压缩宽度;heightdist:压缩高度;
// int benchmark说明:0,长宽哪个长,以哪个为
;1,以宽为基准;2,以高为基准
try {
//System.out.println("*******widthdist********:"+widthdist);
//System.out.println("*******heightdist********:"+heightdist);
//System.out.println("*******benchmark********:"+benchmark);
File srcfile = new File(imgsrc);
if (!srcfile.exists()) {
return;
}
Image src = javax.imageio.ImageIO.read(srcfile);
int width = src.getWidth(null);
int height = src.getHeight(null);
if (width <= widthdist && height <= heightdist) {
// SysUtil.cpoyFile(imgsrc, imgdist);
FileUtils.copyFile(new File(imgsrc), new File(imgdist));
return;
}
// 宽度除以高度的比例
float wh = (float) width / (float) height;
if (benchmark == 0) {
if (wh > 1) {
float tmp_heigth = (float) widthdist / wh;
BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null);
FileOutputStream out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
} else {
float tmp_width = (float) heightdist * wh;
BufferedImage tag = new BufferedImage((int) tmp_width, heightdist,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null);
FileOutputStream out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
}
if (benchmark == 1) {
float tmp_heigth = (float) widthdist / wh;
BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null);
FileOutputStream out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
if (benchmark == 2) {
float tmp_width = (float) heightdist * wh;
BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null);
FileOutputStream out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
logger.error(ex);
}
}
}
ImgPress.reduceImg(this.getUpload().getPath(), dstPath, 200, 180, 0);
本地文件的地址:this.getUpload().getPath()
上传文件的地址:dstPath