public void createSmallPic(int height, int width){ String smallPath = "smallPath"; // 小圖存放目錄 if(!new File(smallPath).exists()) { new File(smallPath).mkdirs(); } double ratio = 0.0; File file = new File("d:/a.jpg"); // 大圖路徑 BufferedImage bi = ImageIO.read(file); // 讀取大圖 if ((bi.getHeight() > height) || (bi.getWidth() > width)) { if (bi.getHeight() > bi.getWidth()) { ratio = (new Integer(height)).doubleValue() / bi.getHeight(); } else { ratio = (new Integer(width)).doubleValue() / bi.getWidth(); } file = new File(smallPath + File.separator + file.getName() + "_" + height + "_" + width + ".jpg"); // 縮圖路徑 Image img = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH); AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null); img = op.filter(bi, null); ImageIO.write((BufferedImage) img, "jpg", file); }}