(图片)jsp上传图片,进行缩放处理
先看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;
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; //设置缩放比例
String dir = "D:\\soft\\Tomcat 6.0\\webapps\\tianjin\\upload\\images";
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();
}
}
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){
}
则在JSP中,先用smartupload上传到服务器上,然后再用上面的代码把它进行缩放以后,或者覆盖原有的图片,或者创建新的文件。
posted on 2012-01-13 10:11 java课程设计例子 阅读(328) 评论(0) 编辑 收藏 举报