public class FileUtils {
private static String netImageToBase64;
/**
* 网络图片路径转base64
*
* @param netImagePath 网络图片路径
* @return: java.lang.String
* @author: LinWeiDa
* @date: 2020/8/20 16:37
**/
public static String ImageToBase64(String netImagePath) {
final ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
// 创建URL
URL url = new URL(netImagePath);
byte[] by = new byte[1024];
// 创建链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
InputStream is = conn.getInputStream();
// 将内容读取内存中
int len = -1;
while ((len = is.read(by)) != -1) {
data.write(by, 0, len);
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
netImageToBase64= encoder.encode(data.toByteArray());
// 关闭流
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return netImageToBase64;
}
}
网络图片地址转Base64
最新推荐文章于 2024-05-31 09:05:13 发布