无损获取网络图片并转换成base64编码

时间:2020-12-10 21:48:25
public String changeImageBase64(String imgUrl){

URL url = null;
BufferedImage image = null;    
try{

url = new URL(imgUrl);         

image = ImageIO.read(url); 

String[] imgType = ImageIO.getReaderMIMETypes();

ByteArrayOutputStream baos = new ByteArrayOutputStream();  

ImageIO.write(image, "png", baos);

byte[] bytes = baos.toByteArray(); 
return Base64.encodeBase64(bytes)
 
}catch (Exception e) {  
e.printStackTrace();