当前位置:网站首页>将OSS上的图片转换成Base64编码
将OSS上的图片转换成Base64编码
2022-04-23 21:59:00 【多汁多味】
使用Java根据oss上保存图片的地址转为Base64编码
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 OssUrlImgBase64Transfer {
public static String getBase64(String ossUrl) {
InputStream in = null;
final ByteArrayOutputStream data = new ByteArrayOutputStream();
//读取图片字节数组
try {
URL url = new URL(ossUrl);
final byte[] by = new byte[1024];
// 创建链接获取图片
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
in = conn.getInputStream();
int len = -1;
while ((len = in.read(by)) != -1) {
data.write(by, 0, len);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
//返回Base64编码过的字节数组字符串
String encode = encoder.encode(data.toByteArray());
encode = encode.replaceAll("[\\s*\t\n\r]", "");
return encode;
}
}
版权声明
本文为[多汁多味]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43652507/article/details/124370390
边栏推荐
- MVC three-tier architecture (illustration)
- 服务雪崩效应
- IIS cannot load * woff,*. woff2,*. Solution of SVG file
- 服务雪崩、服务熔断、服务降级
- Ribbon 服务调用
- 基于Ribbon的服务调用
- The DNN of OpenCV is used to read onnx as the prediction end to realize classification
- Deep understanding of modern mobile GPU (continuously updating)
- Oracle ora-01033: Oracle initialization or shutdown in progressprocess solution
- Idea import a project
猜你喜欢
OpenFeign之响应处理
[leetcode refers to offer 47. Maximum value of gift (medium)]
Opencv application -- jigsaw puzzle
通过OpenFeign传递对象类型参数
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
Ribbon负载均衡策略
Sqlserver edits data in the query interface (similar to Oracle's edit and ROWID)
IIS cannot load * woff,*. woff2,*. Solution of SVG file
Error message: b04access.00f eve'. Read of address 000001B4
Question brushing plan - depth first search (II)
随机推荐
Sklearn function: Train_ Test_ Split (split training set and test set)
[※ leetcode refers to offer 46. Translate numbers into strings (medium)]
Cvpr2022 | efficient pre training based on knowledge distillation
[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]
Keras. Layers introduction to various layers
Database Experiment 3 data update experiment
Normalized transforms in pytorch The real calculation process of normalize
在线时序流程图制作工具
IIS cannot load * woff,*. woff2,*. Solution of SVG file
Mixed use of Oracle column row conversion and comma truncated string
[leetcode refers to offer 21. Adjust the array order so that odd numbers precede even numbers (simple)]
服务熔断的实现
Ribbon组件负载均衡调用和使用
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
分布式系统服务熔断
Common scenes of vector product in image
Pytorch: runtimeerror: an attempt has been made to start a new process Error reporting (resolved)
Express ③ (use express to write interface and cross domain related issues)
Daily operation and maintenance knowledge -- 1
Automatic heap dump using MBean