ByteArrayOutputStream outPut = new ByteArrayOutputStream();
byte[] data = new byte[1024];
try {
// 创建URL
URL url = new URL("***");
// 创建链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10 * 1000);
InputStream inStream = conn.getInputStream();
int len = -1;
while ((len = inStream.read(data)) != -1) {
outPut.write(data, 0, len);
}
byte[] content = outPut.toByteArray();
String encode = Base64.getEncoder().encodeToString(content);
images[i] = "data:image/jpg;base64," + encode.replace("\n","").replace("\r","");
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
java 将网络图片转为base64
最新推荐文章于 2024-08-23 14:24:12 发布