由圖片的網路地址獲取圖片的base64編碼
阿新 • • 發佈:2019-01-23
public String getImageBase64(String imagePath) {
String imgBase64 = null;
try{
URL url;
if(StringUtils.isNotBlank(imagePath)) {
url = new URL(imagePath);
URLConnection uc;
uc = url.openConnection();
uc.connect();
InputStream in;
in = uc.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int n = 0;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
byte[] bytes = out.toByteArray();
imgBase64 = new String(Base64.encodeBase64(bytes));
in.close();
out.close();
}
}catch (Exception e) {
e.printStackTrace();
}
return imgBase64 ;
}