当前位置:网站首页>File的io流与base64
File的io流与base64
2022-06-28 10:50:00 【小玩偶啊】
1.file转换为base64 字符串
@Value("${file.store.path}\\")
private String path;
/**
* 将文件进行base64编码
* @param file
* @return
*/
public String FileToBase64(File file){
String base64 = null;
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(file);
data = new byte[in.available()];
in.read(data);
in.close();
System.out.println(base64);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BASE64Encoder encoder = new BASE64Encoder();
String result=encoder.encode(data);
return result;
}
2. base64字符串转换为file 的io流,并写入本地,返回文件的大小
/**
* 将base64转换为文件并写入本地
*/
public long base64ToFile(String base64Str,String filePath,String fileName) {
long fileSize=0;
if (base64Str == null && filePath == null) {
return 0;
}
try {
filePath=path+filePath;
File file = new File(fileName);
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
if(!file.exists()){
file.createNewFile();
}
filePath=filePath+"\\"+"_"+fileName;
Files.write(Paths.get(filePath), Base64.getMimeDecoder().decode(base64Str), StandardOpenOption.CREATE);
File imageFile = new File(filePath);
fileSize = FileUtils.sizeOf(imageFile);
} catch (IOException e) {
e.printStackTrace();
}
return fileSize;
}边栏推荐
- [Unity]EBUSY: resource busy or locked
- Fabric.js 笔刷到底怎么用?
- Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
- sentinel
- Six fusion positioning technologies in wireless communication application of Internet of things
- Mongo数据库
- 移动命令
- 【NLP】今年高考英语AI得分134,复旦武大校友这项研究有点意思
- [unity][ecs] learning notes (III)
- Internet of things application case of wireless module transparent transmission technology
猜你喜欢
随机推荐
JSON模块、hashlib、base64
Google open source dependency injection framework Guice Guide
Transactions proof in appliedzkp zkevm (10)
MySQL(一)
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
第六天 脚本与动画系统
使用 ABAP 操作 Excel 的几种方法
Ideal interface automation project
To enhance the function of jupyter notebook, here are four tips
[unity][ecs] learning notes (II)
Cisco * VRF (virtual route forwarding table)
The boss asked me to write an app automation -- yaml file reading -- with the whole framework source code attached
2022 Wu Enda machine learning specialization week 2 practice lab: linear expression
如何利用k线图做技术分析
Threads and thread pools
树莓派无需显示屏的VNC Viewer方式的远程连接
Fastposter v2.8.4 release e-commerce poster generator
Fastposter v2.8.4 release e-commerce poster generator
fastposter v2.8.4 发布 电商海报生成器



![[Unity]内置渲染管线转URP](/img/a5/3ae37b847042ffb34e436720f61d17.png)




