java Thumbnails 加载网络图片,处理返回base64

URL url = new URL("图片网络地址");
BufferedInputStream in = new BufferedInputStream(url.openStream());

BufferedImage thumbnailBI = Thumbnails.of(in).scale(0.5f);

String imageBase64 = imageThumbnailsToBase64(thumbnailBI, imageNewName);

//转换
public String imageThumbnailsToBase64(BufferedImage bI, String imageName) {
String back = "true";
try {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imageOut;

imageOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bI, "jpg", imageOut);

// 图片转换为base64并返回
BASE64Encoder encoder = new sun.misc.BASE64Encoder();
byte[] bytes = bs.toByteArray();
return encoder.encodeBuffer(bytes).trim();

} catch (Exception e) {
back = "false";
System.out.println("Thumbnails Upload failed:" + e);
}

return back;
}

  

posted on 2016-06-03 11:04  OceanWang  阅读(1063)  评论(0编辑  收藏  举报

导航