当前位置:网站首页>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编码中会被解码成空格)
边栏推荐
- Design and simulation code of 4-bit subtracter based on FPGA
- Floating point addition and subtraction method of vivado IP core floating point
- How to use SFTP command to access SFTP server on the development board
- Merkletree builds QT implementation UI
- Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (3) -- modk routine test
- 注解(Annotation)
- 浅谈缺陷描写样式
- FIR滤波器设计(1)——利用matlab的fdatool工具箱设计FIR滤波器参数
- 网站被挂马的解决方案
- Hongke education you want to enter the field of TSN? Hongke teaches you how to build TSN test system
猜你喜欢

Vivado IP核之复数浮点数乘法 Floating-point

基于FPGA的IIR型滤波器设计

Floating point multiplication and division of vivado IP core floating point

Hongke education you want to enter the field of TSN? Hongke teaches you how to build TSN test system

Arrays & object & System & Math & random & Packaging

Floating point addition and subtraction method of vivado IP core floating point

华为交换机CE12808导入导出配置文件

day16-集合上

三、广域通信网

C语言内存-栈与堆使用
随机推荐
day06_ Classes and objects
Merkle tree existential function modified for the first time
网站被挂马的解决方案
DDoS攻击与CC攻击的区别
OpenResty的核心与cosocket
Merkletree builds QT implementation UI
day04_数组
Circular linked list and bidirectional linked list
Understand the great changes of network security in five years
What is the basic principle of Library collision and library collision attack
Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
盘点 | 全球关键信息基础设施网络安全大事件
Huawei switch ce12808 import and export configuration file
Hongke will share the EtherCAT demo for you and teach you how to quickly transition from other protocols to EtherCAT industrial bus
Online multiplayer chat room based on UDP communication
3、 Wide area communication network
Tcp/ip 五层参考模型以及对应的典型设备以及ipv6
FIR filter design (1) -- using the FDATool toolbox of MATLAB to design FIR filter parameters
比较单片机3种时钟电路方案
Shell脚本-全局变量、局部变量、环境变量