当前位置:网站首页>在线文件不落地转base64
在线文件不落地转base64
2022-06-11 10:31:00 【差点资深程序员】
/**
* 文件链接转base64
* @param urlStr 文件在线url
* @return 文件base64格式
*/
public static String fileToBase64(String urlStr){
String base64 = null;
try {
// 统一资源
URL url = new URL(urlStr);
// 连接类的父类,抽象类
URLConnection urlConnection = url.openConnection();
// http的连接类
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
//设置超时
httpURLConnection.setConnectTimeout(1000 * 5);
//设置请求方式,默认是GET
httpURLConnection.setRequestMethod("POST");
// 设置字符编码
httpURLConnection.setRequestProperty("Charset", "UTF-8");
// 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
httpURLConnection.connect();
// 文件大小
int fileLength = httpURLConnection.getContentLength();
// 控制台打印文件大小
System.out.println("您要下载的文件大小为:" + fileLength / (1024 * 1024) + "MB");
// 建立链接从请求中获取数据
URLConnection con = url.openConnection();
BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = bin.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
bin.close();
byte[] bytes = outStream.toByteArray();
BASE64Encoder base64Encoder = new BASE64Encoder();
System.out.println("base64" + base64Encoder.encode(bytes));
bin.close();
base64 = base64Encoder.encode(bytes);
} catch (IOException e) {
e.printStackTrace();
}
return base64;
}边栏推荐
- Circuit board made of real gold -- golden finger
- Tiktok encounters cultural conflict in the UK, and many employees leave in a short time
- Puppeter class of puppeter introduction
- Some code fragments of a universal and confession wall system developed by PHP
- golang编译和链接参数,运行时
- NGUI,地图放大缩小
- Mysql--索引
- Database design and paradigm explanation
- [audio and video] Introduction to SEI
- 1. system in Library
猜你喜欢

用真金做的电路板——金手指

Leetcode 1995. 统计特殊四元组(暴力枚举)

Introduction to ZigBee module wireless transmission star topology networking structure

金仓数据KingbaseES 批量创建数据库对象

pyspark案例系列4-dataframe输出到单个文件夹的解决方案

远程监控项目离线日志说明书

Pyspark case series 4-dataframe output to a single folder solution

Global pooling – pytoch

Arbitrum infrastructure: a quick start

Tiktok encounters cultural conflict in the UK, and many employees leave in a short time
随机推荐
Tree topology networking structure of ZigBee module communication protocol
What is digital twin? A real-time and virtual representation
rpc的正确打开方式|读懂Go原生net/rpc包
Safety related website recommendations
Tiktok encounters cultural conflict in the UK, and many employees leave in a short time
MySQL permission management and backup
Xilinx pin constraint file xdc
Mouse click coordinate transformation generation
minIni移植到littlefs
C # introductory series (11) -- multidimensional array
[MySQL] use of stored procedures
Wuenda machine learning course - week 7
知识点滴 - 性格分析-四类法
以银行异业合作为例,浅谈小程序生态的建设
新西兰是道路安全做的最好的国家之一
MD5 learning
UGUI
班组级安全培训,新员工入职培训教育课件,全内容PPT套用
Wechat applet ordering system with source code
NGUI,冷却效果