当前位置:网站首页>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编码中会被解码成空格)
边栏推荐
- LVM逻辑卷组的管理
- 解决分频模块modelsim下仿真输出为stx的错误
- FIR滤波器设计(2)——Vivado调用IP核设计FIR滤波器
- day12_ Multithreading
- IGMP protocol software development experiment
- Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (3) -- modk routine test
- Hongke | uses jesd204 serial interface to bridge analog and digital worlds at high speed
- Leetcode question brushing record
- 关于DDoS的几个误区
- What is the basic principle of Library collision and library collision attack
猜你喜欢

day10_异常处理&枚举

VMware虚拟机在物理机win10系统下如何连接外网

FIR滤波器设计(2)——Vivado调用IP核设计FIR滤波器

day14_ Unit test & Date common class & String common class

Tcp/ip 五层参考模型以及对应的典型设备以及ipv6

Joint use skills of joiner.on and stream().Map

Hongke shares | why EtherCAT is the best solution to improve the performance of the control system?

软件测试的优势有哪些?看看你了解多少.....

Sequence list and linked list

Hongke share | FPGA implementation of pass through and store and forward switching delay
随机推荐
Day16 set
Hongke will share the EtherCAT demo for you and teach you how to quickly transition from other protocols to EtherCAT industrial bus
vmstat 内存消耗查询
4、 LAN and man
What is WAF protection
六、 网络互联与互联网
Six common ways for hackers to attack servers
day06_ Classes and objects
多路IO用法
Complex floating point multiplication of vivado IP core floating point
day15_ generic paradigm
day03_1_流程控制
Design and simulation code of 4-bit subtracter based on FPGA
Floating point square root of vivado IP core floating point
Thinking about MySQL taking shell through OS shell
Vivado IP核之复数浮点数乘法 Floating-point
8、 Network security
Hongke solution | a unique solution to realize seamless integration at low cost in Digital Substations
Right value reference and mobile construction
VMware虚拟机在物理机win10系统下如何连接外网