//缩略图实现,将图片(jpg,gif,bmp等等)真实的变成想要的大小 
import com.sun.image.codec.jpeg.JPEGCodec; 
 import com.sun.image.codec.jpeg.JPEGEncodeParam; 
 import com.sun.image.codec.jpeg.JPEGImageEncoder; 
 import java.awt.Graphics; 
 import java.awt.Image; 
 import java.awt.image.BufferedImage; 
 import java.io.File; 
 import java.io.FileOutputStream; 
 import java.io.IOException; 
 import javax.imageio.ImageIO; 
 import javax.swing.ImageIcon; //Java图片缩小后不失真的代码(缩略图) 
public class thumbs { 
/** 
     * Resize a JPEG file and save it. 
   
     * @param inputfile complete path to original jpeg file 
     * @param outputfile complete save path 
     * @param newWidth width in pixel 
     * @param newHeight height in pixel 
     * @return success of resize and save 
     */ 
    boolean isDeleted=false;
    public  String resize(String inputfile, String outputfile, int newWidth, int newHeight,boolean del) { 
              this.isDeleted=del;
            try { 
                Image img = new ImageIcon(ImageIO.read(new File(inputfile))).getImage(); 
                if (img.getWidth(null) == -1) {   
                    System.out.println(" can't read,retry!" + "<BR>");    
                    return "no"; 
                }
                if((img.getHeight(null)<newHeight)&&(img.getWidth(null)<newWidth))  //如果图片高度和宽度小于设定,则不处理
                { return "small pic,not to deal with!";}
                else
                   {  if (this.isDeleted)  //确定是否要删除原文件
                      {
                      File file1 = new File(inputfile);
                      if(file1.exists() && file1.isFile())
                       {     file1.delete(); }
                      }
                   }
                if((img.getHeight(null)>img.getWidth(null))&&(img.getHeight(null)>480))
                { newWidth=480;newHeight=640;}                double rate1 = ((double) img.getWidth(null)) / (double)newWidth + 0.1;    
                double rate2 = ((double) img.getHeight(null)) / (double)newHeight + 0.1;    
                // 根据缩放比率大的进行缩放控制    
                double rate = rate1 > rate2 ? rate1 : rate2;    
                newWidth = (int) (((double) img.getWidth(null)) / rate);    
                newHeight = (int) (((double) img.getHeight(null)) / rate);    
                
                Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); 
                BufferedImage outImg = new BufferedImage(newWidth, newHeight, BufferedImage.SCALE_SMOOTH);
                Graphics g = outImg.getGraphics(); 
                g.drawImage(scaledImage, 0, 0, null); 
                g.dispose(); 
  
                JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(outputfile)); 
                JPEGEncodeParam param = jpegImageEncoder.getDefaultJPEGEncodeParam(outImg); 
                param.setQuality(0.7F, true); 
                jpegImageEncoder.encode(outImg, param); 
                return "OK!"; 
            } catch (IOException e) { 
                e.printStackTrace(); 
                return "failed!"; 
            }     }  }