当前位置:网站首页>IO stream of file and Base64
IO stream of file and Base64
2022-06-28 11:43:00 【Little doll】
1.file Convert to base64 character string
@Value("${file.store.path}\\")
private String path;
/**
* Change the file base64 code
* @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 String conversion to file Of io flow , And write local , Returns the size of the file
/**
* take base64 Convert to file and write locally
*/
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;
}边栏推荐
猜你喜欢

day33 js笔记 事件(下)2021.09.28

人人都可以参与开源!龙蜥社区最不容错过的开发者活动来了
This Exception was thrown from a job compiled with Burst, which has limited exception support. report errors

day29 js笔记 2021.09.23

Day39 prototype chain and page fireworks effect 2021.10.13

day39 原型鏈及頁面烟花效果 2021.10.13

ThreadLocal的简单理解

Day31 JS notes DOM 2021.09.26

赛尔号抽奖模拟求期望

Simulation of the Saier lottery to seek expectation
随机推荐
李宏毅《机器学习》丨7. Conclusion(总结)
Day29 JS notes 2021.09.23
智联招聘基于 Nebula Graph 的推荐实践分享
《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!
Intranet penetration in the working group environment: some basic methods
How to distinguish and define DQL, DML, DDL and DCL in SQL
毕业了
Compareto() and equals() methods of BigDecimal class
行业分析| 快对讲,楼宇对讲
[sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
Contract quantitative trading system development | contract quantitative app development (ready-made cases)
Solve the problem of reading package listsdonebuilding dependency treereading state informationdone
2022中国信通院首届业务与应用安全发展论坛成功召开!
Day24 JS notes 2021.09.15
东方财富手机股票开户哪个券商更安全更方便?
[semidrive source code analysis] [x9 chip startup process] 32 - play module analysis - RTOS side
Characteristics of solar wireless LED display
Dongyuhui, New Oriental and Phoenix Satellite TV
Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022
近况