Java言語でサムネイルをアップロード(スケーリングによる判断)

2014年6月14日

サンプルコード:

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;
is = new FileInputStream(upload);
File file=new File(fileDir);
if(!file.exists()){
file.mkdirs();
}
try {
File reviaryFile = new File(fileDir + “small20130606.jpg");
AffineTransform transform = new AffineTransform();
bis = ImageIO.read(is);
int w = bis.getWidth();
int h = bis.getHeight();
int newWidth = (int) (w * 0.1D);
int newHight = (int) (h * 0.1D);
transform.setToScale(0.1, 0.1);
AffineTransformOp ato = new AffineTransformOp(transform, null);
BufferedImage bid = new BufferedImage(newWidth, newHight, 5);
ato.filter(bis, bid);
ImageIO.write(bid, “jpg", reviaryFile);
} catch (Exception e) {
System.out.println(“サムネイルをアップロードできませんでした");
}finally{
if (is != null) {
is.close();
}
}
return fileDir;
}

 

Source

Posted by arkgame