当前位置:网站首页>图片地址转为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);
}
}
边栏推荐
- AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
- binary search tree problem
- Hong Kong International Jewellery Show and Hong Kong International Diamond, Gem and Pearl Show kick off
- Flink Learning 12: DataStreaming API
- RK3568环境安装
- 不太会讲爱,其实已经偷偷幸福很久啦----我们的故事
- UDP broadcast
- Re regular expressions
- 【工具配置篇】VSCode 常用使用总结
- Shiny04---Application of DT and progress bar in shiny
猜你喜欢
随机推荐
DeFi 前景展望:概览主流 DeFi 协议二季度进展
2022 crane driver (limited bridge crane) exam question bank and simulation test
Shiny02---Shiny exception solution
C# FileSystemWatcher
怎么样避免线上内存泄漏
MySQL: basic part
Flink学习12:DataStreaming API
二叉搜索树问题
【LeetCode】235.二叉搜索树的最近公共祖先
Libpq 是否支持读写分离配置
In the anaconda Promat interface, import torch is passed, and the error is reported in the jupyter notebook (only provide ideas and understanding!)
Does Libpq support read-write separation configuration?
MySQL: JDBC programming
TCP sticky packet unpacking problem + solution
typescript62-泛型工具类型(record)
2022.7.29好题选讲(计数专题)
typescript66-分析partial的实现
Task flow scheduling tool AirFlow,, 220804,,
typescript67-索引查询类型
2022熔化焊接与热切割操作证考试题及模拟考试