先看java代码:
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
try{
String fileName = "D:\\soft\\Tomcat 6.0\\webapps\\XXX\\upload\\images\\me.jpg";
File f = new File(fileName); //得到原始文件
double newHeight = 1.0; //设置缩放比例
double newWeight = 1.0; //设置缩放比例
File f = new File(fileName); //得到原始文件
double newHeight = 1.0; //设置缩放比例
double newWeight = 1.0; //设置缩放比例
String dir = "D:\\soft\\Tomcat 6.0\\webapps\\tianjin\\upload\\images";
File newFile = new File(dir,"me.jpg"); //新的文件路径及名字
File newFile = new File(dir,"me.jpg"); //新的文件路径及名字
BufferedImage bi = ImageIO.read(f);
//假设图片宽 高 最大为120 120
Image itemp = bi.getScaledInstance (120,120,bi.SCALE_SMOOTH);
if((bi.getHeight()>120) || (bi.getWidth()>120)){
if (bi.getHeight()>bi.getWidth()){
newHeight = 120.0/bi.getHeight();
}else{
newWeight = 120.0/bi.getWidth();
}
}
www.2cto.com
//假设图片宽 高 最大为120 120
Image itemp = bi.getScaledInstance (120,120,bi.SCALE_SMOOTH);
if((bi.getHeight()>120) || (bi.getWidth()>120)){
if (bi.getHeight()>bi.getWidth()){
newHeight = 120.0/bi.getHeight();
}else{
newWeight = 120.0/bi.getWidth();
}
}
www.2cto.com
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(newHeight, newWeight), null);
itemp = op.filter(bi, null);
try {
ImageIO.write((BufferedImage)itemp,"jpg", newFile);
System.out.println("well done");
}catch (Exception ex) {
ex.printStackTrace();
}
}catch(Exception e){
itemp = op.filter(bi, null);
try {
ImageIO.write((BufferedImage)itemp,"jpg", newFile);
System.out.println("well done");
}catch (Exception ex) {
ex.printStackTrace();
}
}catch(Exception e){
}
则在JSP中,先用smartupload上传到服务器上,然后再用上面的代码把它进行缩放以后,或者覆盖原有的图片,或者创建新的文件。
摘自 漂泊小柒的专栏