当前位置:网站首页>图片地址转为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);
}
}
边栏推荐
- [Shanghai] Hiring .Net Senior Software Engineer & BI Data Warehouse Engineer (Urgent)
- After the firewall iptable rule is enabled, the system network becomes slow
- Week 8 Document Clustering(文本聚类)
- Cannot compare or sort text, ntext, and image data types
- 线程池的使用(结合Future/Callable使用)
- DeFi 前景展望:概览主流 DeFi 协议二季度进展
- Technical Analysis Mode (7) Playing the Gap
- 2022起重机司机(限桥式起重机)考试题库及模拟考试
- 蓝牙gap协议
- FPGA parsing B code----serial 4
猜你喜欢
Week 8 Document Clustering
protobuf is compiled against the associated .proto file
DeFi 前景展望:概览主流 DeFi 协议二季度进展
TRACE32——外设寄存器查看与修改
算法拾遗十五补链表相关面试题
Flink Learning 12: DataStreaming API
Modeling of the MAYA ship
一天学会从抓包到接口测试,通过智慧物业项目深度解析
TRACE32——Break
After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career
随机推荐
UDP group (multi)cast
Why does Mysql fail to create a database
2022.7.29好题选讲(计数专题)
TRACE32——List源代码查看
Rapid Medical's Ultra-Small and Only Adjustable Thromb Retriever Receives FDA Clearance
Flink学习10:使用idea编写WordCount,并打包运行
How to avoid online memory leaks
Redis
IO进程线程->进程间的通信->day7
Put Cloudflare on the website (take Tencent Cloud as an example)
C# FileSystemWatcher
MySQL: join query | inner join, outer join
Technical Analysis Mode (8) Double Top and Bottom
不太会讲爱,其实已经偷偷幸福很久啦----我们的故事
Cannot compare or sort text, ntext, and image data types
栈与队列的基本介绍和创建、销毁、出入、计算元素数量、查看元素等功能的c语言实现,以及栈的压入、弹出序列判断,栈结构的链式表示与实现
re正则表达式
protobuf is compiled against the associated .proto file
Using printf function in STM32
[Shanghai] Hiring .Net Senior Software Engineer & BI Data Warehouse Engineer (Urgent)