当前位置:网站首页>工具类:base64格式的数据与本地文件的相互转换
工具类:base64格式的数据与本地文件的相互转换
2022-08-09 02:21:00 【abs(ln(1+NaN))】
public class UploadUtil {
//这里的pathName 必须具体到某一张图片或者某一个文件的名字,如 D:\\xxx\\xxx.jpg
public static void storeImageAtLocal(String base64ImgData,String pathName){
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(base64ImgData);
/*
* 字节流转文件
*/
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pathName);
fos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//本地图片转base64编码数据
public static String getImageBase64Data(String localPath){
File file = new File(localPath); //URL url = new URL(src);
byte[] data = null;
try {
InputStream in = new FileInputStream(file); //InputStream in = url.openStream();
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(data);
}
}
边栏推荐
- 方法参数
- 显著性检验--学习笔记
- 使网络安全威胁风险更高和成本更高的五个趋势
- spdlog日志库的封装使用
- D. Tournament Countdown
- New Swagger3.0 tutorial, OAS3 quick configuration guide, to automate API interface documentation!
- MT4/MQL4入门到精通EA课程第二课-常用的功能函数
- 企业从云服务的承诺支出中获得最大收益的四种方法
- 【HNUMSC】C语言第二讲
- MT4/MQL4 entry to proficient foreign exchange EA tutorial Lesson 1 Getting to know MetaEditor
猜你喜欢
Summary of Database Design
9.1-----24. Swap the nodes in the linked list in pairs
Group DETR:分组一对多匹配是加速DETR收敛的关键
2.1-----27. Remove elements
ROS2错误:不支持OpenGL 1.5 GLRenderSystem:: ci initialiseContext在C: \ \ ws \构建……
考研人总结的时间管理7大忌,你中了几条?
js实现数组去重的方式(7种)
MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor
Codeforces Round #809 (Div. 2)A~D1
力扣刷题记录9.1-----24. 两两交换链表中的节点
随机推荐
final
2022 Eye Care Products Exhibition, Beijing Eye Health Exhibition, Ophthalmology Exhibition, Myopia Correction Equipment Exhibition
MT4/MQL4 Getting Started to Mastering EA Tutorial Lesson 1 - MQL Language Common Functions (1) OrderSend() Function
2022护眼产品展,北京眼健康展,眼科医学展,近视矫正设备展
D. Tournament Countdown
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
Design of Go-7-RESTful API
17.flink Table Api基础概念讲解
2020.12.4 log
软件开发之我的一点想法
力扣刷题记录8.1-----206. 反转链表
2022/8/8 比赛思维+状压dp
力扣刷题记录3.1-----977. 有序数组的平方
Etcd realize large-scale application service management of actual combat
eladmin容器部署超详细过程
Summary of pytorch related knowledge points
[ANT]apache ant 安装说明
[C language brush questions] Application of fast and slow pointers in linked lists
力扣刷题记录5.1-----59. 螺旋矩阵 II
How to play knowledge graph in recommender system