当前位置:网站首页>图片地址转为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);
}
}
边栏推荐
- typescript61-泛型工具类型(pick)
- TRACE32——C源码关联1
- 本地能ping通虚拟机,虚拟机ping不通本地
- 给网站套上Cloudflare(以腾讯云为例)
- typescript63-索引签名类型
- Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance
- HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
- Database table insert data
- 游戏思考19:游戏多维计算相关:点乘、叉乘、点线面距离计算
- typescript67-索引查询类型
猜你喜欢
随机推荐
给网站套上Cloudflare(以腾讯云为例)
(4) Rotating object detection data roLabelImg to DOTA format
女生做软件测试会不会成为一个趋势?
TRACE32——List源代码查看
In the anaconda Promat interface, import torch is passed, and the error is reported in the jupyter notebook (only provide ideas and understanding!)
工作3年,回想刚入门和现在的今昔对比,笑谈一下自己的测试生涯
AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
游戏思考19:游戏多维计算相关:点乘、叉乘、点线面距离计算
Shiny04---Application of DT and progress bar in shiny
360度反馈调查表中的问题示范
Mysql为什么 建立数据库失败
Mysql主从延迟的原因和解决方案
1、Citrix XenDesktop 2203之AD域系统安装(一)
数据库多表关联插入数据
[Tool Configuration] Summary of Common Uses of VSCode
MAYA大炮建模
Redis
cmake 学习使用笔记(三)
1, Citrix XenDesktop 2203 AD domain system installation (1)
真实字节跳动测试开发面试题,拿下年薪50万offer。









