当前位置:网站首页>图片地址转为base64
图片地址转为base64
2022-08-05 06:51:00 【null111666】
package com.newcapec.custom.utils;
import sun.misc.BASE64Encoder;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetPhotoBase64Util {
private static String strNetImageToBase64;
public static void main(String[] args) {
String aa=getImageBase("url");
System.out.println(aa);
}
public static String getImageBase(String src) {
InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
byte[] buffer = null;
try {
// 创建URL
URL url = new URL(src);
// 创建链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
inputStream = conn.getInputStream();
outputStream = new ByteArrayOutputStream();
// 将内容读取内存中
buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
buffer = outputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
// 关闭inputStream流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
// 关闭outputStream流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 对字节数组Base64编码
return new BASE64Encoder().encode(buffer);
}
}
边栏推荐
猜你喜欢
随机推荐
400 times performance improvement 丨 swap valuation optimization case calculation
Mysql为什么 建立数据库失败
性能提升400倍丨外汇掉期估值计算优化案例
protobuf is compiled against the associated .proto file
3555. 二叉树
MySQL: join query | inner join, outer join
给网站套上Cloudflare(以腾讯云为例)
2022熔化焊接与热切割操作证考试题及模拟考试
Database table insert data
共享内存+inotify机制实现多进程低延迟数据共享
1、Citrix XenDesktop 2203之AD域系统安装(一)
开启防火墙iptable规则后,系统网络变慢
UDP组(多)播
mysql使用in函数的一个小问题
typescript64-映射类型
【JVM调优】Xms和Xmx为什么要保持一致
Redis
Tencent Business Security Post IDP Talk Summary
[Tool Configuration] Summary of Common Uses of VSCode
原来使Maya Arnold也能渲染出高质量作品!超赞小技巧









