Java言語でサムネイルをアップロード(指定した比例による判断)

サンプルコード:

public String uploadFile(File upload, String reaName,
String uploadType) throws IOException {
String uploadpath="D:/csg";
String uploadpath2="/upload/image/small/";
String fileDir = uploadpath +uploadpath2;
InputStream is = null;
BufferedImage bis = null;
Image srcUpload = null;
is = new FileInputStream(upload);
File file=new File(fileDir);
if(!file.exists()){
file.mkdirs();
}
try {
srcUpload = ImageIO.read(is);
int newWidth = 0, newHight = 0;
int w = srcUpload.getWidth(null);
int h = srcUpload.getHeight(null);
if (w >= h) {
newWidth = 90;
newHight = h * 90 / w;
} else {
newHight = 90;
newWidth = w * 90 / h;
}
BufferedImage image = new BufferedImage(newWidth, newHight,
BufferedImage.TYPE_3BYTE_BGR);
image.getGraphics().drawImage(srcUpload.getScaledInstance(newWidth, newHight, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(fileDir + “/" + “small123.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
image.flush();
out.flush();
out.close();
System.gc();
} catch (Exception e) {
System.out.println(“サムネイルをアップロードできませんでした");
}finally{
if (is != null) {
is.close();
}
}
return fileDir;
}

Source

Posted by arkgame