由图片的网络地址获取图片的base64编码

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 ;

}

你可能感兴趣的:(java开发问题)