当前位置:网站首页>在线文件不落地转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;
}边栏推荐
猜你喜欢

Global pooling – pytoch

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

Jedislock redis distributed lock implementation

What is the SOA or ASO of MOSFET?

NFT产品是有生命的

No more! The entry byte beat for a week and ran decisively.

Tree topology networking structure of ZigBee module communication protocol

Installing mysql5.7 for Linux

Pl/sql compilation check in kingbasees

用真金做的电路板——金手指
随机推荐
Correct opening method of RPC | understand go native net/rpc package
硬件描述语言HDL
Use bat to write to the first line of the file
Fix the problem that uicollectionview does not reach the bottom security zone
Knowledge drop - personality analysis - four types of method
电子设备辐射EMC整改案例
UGUI
Pyramidtnt: TNT with characteristic pyramid structure
Dotween usage
MySQL 权限管理和备份
Puppeter class of puppeter introduction
新西兰是道路安全做的最好的国家之一
Arbitrum 基础架构:快速入门
IPhone 15 forced to use type-C interface
使用bat向文件的第一行中写入内容
Dimension types for different CV tasks
VMware install win7 virtual machine
C语言课程设计
NFT将改变元宇宙中的数据所有权
Some understanding of inductive bias