当前位置:网站首页>Base64与File之间的相互转化
Base64与File之间的相互转化
2022-07-29 05:27:00 【魔道不误砍柴功】
问题:最近遇到一个上传文件的问题,前端使用了另一种传值,就是Base64字符串传给后台 ,一开始没有对其进行解码操作,存入数据库时就超长了,今天这里提供一种base64和file之间相互转化的工具类,以便日后参考
/**
*
* @param path
* @return String
* @description 将文件转base64字符串
* @date 2018年3月20日
* @author changyl
* File转成编码成BASE64
*/
public static String fileToBase64(String path) {
String base64 = null;
InputStream in = null;
try {
File file = new File(path);
in = new FileInputStream(file);
byte[] bytes=new byte[(int)file.length()];
in.read(bytes);
base64 = Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}
//BASE64解码成File文件
public static void base64ToFile(String destPath,String base64, String fileName) {
File file = null;
//创建文件目录
String filePath=destPath;
File dir=new File(filePath);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
BufferedOutputStream bos = null;
java.io.FileOutputStream fos = null;
try {
byte[] bytes = Base64.getDecoder().decode(base64);
file=new File(filePath+"/"+fileName);
fos = new java.io.FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
*需要注意:标红的base64在这里需要去掉
baseStr = baseStr.replace("data:image/jpeg;base64,", "");//base64解密部分乱码问题(“+” 号,在urlecode编码中会被解码成空格)
边栏推荐
- find命令详解(文章最后运维最常用操作)
- Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (3) -- modk routine test
- day17_集合下
- Six common ways for hackers to attack servers
- FPGA—奇偶数分频和小数分频代码例程
- Complex floating point multiplication of vivado IP core floating point
- The difference between DDoS attack and CC attack
- 如何画出优秀的架构图
- centos 部署postgresql 13
- MerkleTree 构建QT实现UI
猜你喜欢
day15_ generic paradigm
Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?
Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
day09_ Static & Final & code block & abstract class & Interface & internal class
将源码包转换为rpm包
After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?
6、 Network interconnection and Internet
FIR滤波器设计(1)——利用matlab的fdatool工具箱设计FIR滤波器参数
LDAP简述及统一认证说明
Hongke | uses jesd204 serial interface to bridge analog and digital worlds at high speed
随机推荐
Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (2) -- modk operation environment construction
FIR filter design (1) -- using the FDATool toolbox of MATLAB to design FIR filter parameters
7、 Next generation Internet IPv6
2022年的软件测试的岗位要求为何越来越高?这其中有什么不可告人的秘密吗?
Merkletree builds QT implementation UI
What is the lifecycle of automated testing?
Common server faults and their solutions
【面试题】2022年最新软件测试面试题(400道)【附带答案】持续更新...
解决分频模块modelsim下仿真输出为stx的错误
Idea practical shortcut key novice must see
Use of for statement in Verilog
通过os-shell引发的mysql拿shell的思考
greenplum企业部署
关于DDoS的几个误区
如何在开发板上使用sftp命令访问sftp-server
Using STP spanning tree protocol to solve the problem of two-layer loop in network
day12_多线程
Right value reference and mobile construction
Software testing career development: where should software testers go
VMware虚拟机在物理机win10系统下如何连接外网