当前位置:网站首页>图片地址转为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+视频技术助力保障校园安全,校园智能安防平台该如何建设?
- It turns out that Maya Arnold can also render high-quality works!Awesome Tips
- LaTeX Notes
- FPGA parsing B code----serial 4
- How to avoid online memory leaks
- 给网站套上Cloudflare(以腾讯云为例)
- "Automatic Data Collection Based on R Language"--Chapter 3 XML and JSON
- GAN生成动漫头像Pytorch
- 691. 立方体IV
- Advanced Redis
猜你喜欢

UDP broadcast

IO process thread -> communication between processes -> day7

真实字节跳动测试开发面试题,拿下年薪50万offer。

TRACE32——Go.direct

Put Cloudflare on the website (take Tencent Cloud as an example)

Tencent Internship Summary

TRACE32——外设寄存器查看与修改

Bluetooth gap protocol

typescript61-泛型工具类型(pick)

FPGA parsing B code----serial 4
随机推荐
Technical Analysis Mode (7) Playing the Gap
二叉搜索树问题
Libpq 是否支持读写分离配置
基于快速行进平方法的水面无人船路径规划
protobuf根据有关联的.proto文件进行编译
TCP sticky packet unpacking problem + solution
17-VMware Horizon 2203 虚拟桌面-Win10 手动桌面池浮动(十七)
Advanced Redis
环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机
专用机终端安装软件后报IP冲突
re正则表达式
基于KECA-IGWO-KELM的间歇过程故障诊断方法
binary search tree problem
【instancetype类型 Objective-C】
TRACE32——加载符号表信息用于调试
Hash 这些知识你也应该知道
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
Flink学习12:DataStreaming API
Bluetooth gap protocol
MySQL:order by排序查询,group by分组查询