First look at the java code:
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); // obtain the original File
Double newHeight = 1.0; // sets the zoom ratio.
Double newWeight = 1.0; // sets the zoom ratio.
String dir = "D :\\ soft \ Tomcat 6.0 \ webapps \ tianjin \ upload \ images ";
File newFile = new File (dir, "me.jpg"); // new File path and name
BufferedImage bi = ImageIO. read (f );
// Assume the maximum image width and height is 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 ){
}
In JSP, you first use smartupload to upload the file to the server, then use the above Code to scale it, overwrite the original image, or create a new file.
Excerpted from the column of wandering hacker