Java中Base64和File之間互轉(zhuǎn)代碼示例
1、Base64 轉(zhuǎn) File
public File base64ToFile(String base64, String filePath) {
File file = new File(filePath);
byte[] buffer;
try {
BASE64Decoder base64Decoder = new BASE64Decoder();
buffer = base64Decoder.decodeBuffer(base64);
FileOutputStream out = new FileOutputStream(filePath);
out.write(buffer);
out.close();
} catch (Exception e) {
Log.e("TAG", "異常信息:" + e.getMessage());
}
return file;
}2、File 轉(zhuǎn) Base64
public String fileToBase64(String filePath) {
File file = new File(filePath);
FileInputStream inputFile;
try {
inputFile = new FileInputStream(file);
byte[] buffer = new byte[inputFile.available()];
inputFile.read(buffer);
inputFile.close();
BASE64Encoder base64Encoder = new BASE64Encoder();
Log.i("encodeFileToBase64", "encode = " + base64Encoder.encode(buffer));
return base64Encoder.encode(buffer);
} catch (Exception e) {
Log.e("TAG", "異常信息:" + e.getMessage());
}
return "";
}附:java把base64位編碼轉(zhuǎn)為File文件并存到本地
public File getFileFromBase64(String base) throws Exception {
String base64Pic = base;
File file = null;
Map<String, Object> resultMap = new HashMap<String, Object>();
if (base64Pic == null) { // 圖像數(shù)據(jù)為空
resultMap.put("resultCode", 0);
resultMap.put("msg", "圖片為空");
} else {
BASE64Decoder decoder = new BASE64Decoder();
String baseValue = base64Pic.replaceAll(" ", "+");//前臺(tái)在用Ajax傳base64值的時(shí)候會(huì)把base64中的+換成空格,所以需要替換回來(lái)。
byte[] b = decoder.decodeBuffer(baseValue.replace("data:image/jpeg;base64,", ""));//去除base64中無(wú)用的部分
base64Pic = base64Pic.replace("base64,", "");
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
String nowDate = df2.format(new Date());
String imgFilePath = QMConfig.filePathRoot + "\\" + nowDate + "\\" + System.currentTimeMillis();
File file1 = new File(imgFilePath);
if (!file1.exists() && !file1.isDirectory()) {//判斷文件路徑下的文件夾是否存在,不存在則創(chuàng)建
file1.mkdirs();
}
try {
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 調(diào)整異常數(shù)據(jù)
b[i] += 256;
}
}
file = new File(imgFilePath + "\\" + System.currentTimeMillis());
// 如果要返回file文件這邊return就可以了,存到臨時(shí)文件中
OutputStream out = new FileOutputStream(file.getPath());
out.write(b);
out.flush();
out.close();
} catch (Exception e) {
resultMap.put("resultCode", 0);
resultMap.put("msg", "存儲(chǔ)異常");
}
}
return file;
}總結(jié)
到此這篇關(guān)于Java中Base64和File之間互轉(zhuǎn)的文章就介紹到這了,更多相關(guān)Java Base64和File互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud OAuth2 實(shí)現(xiàn)用戶認(rèn)證及單點(diǎn)登錄的示例代碼
這篇文章主要介紹了Spring Cloud OAuth2 實(shí)現(xiàn)用戶認(rèn)證及單點(diǎn)登錄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
解析電子郵件的基本概念及JavaMail API郵件功能使用
這篇文章主要介紹了電子郵件的基本概念及JavaMail API郵件功能使用,包括用Java來(lái)發(fā)送郵件的示例,需要的朋友可以參考下2016-02-02
關(guān)于Kill指令停掉Java程序的問(wèn)題
這篇文章主要介紹了Kill指令停掉Java程序的思考,主要探究kill指令和java的關(guān)閉鉤子的問(wèn)題,需要的朋友可以參考下2021-10-10
Springboot3.3 整合Cassandra 4.1.5的詳細(xì)過(guò)程
這篇文章主要介紹了Springboot3.3 整合Cassandra 4.1.5的詳細(xì)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
基于springboot處理date參數(shù)過(guò)程解析
這篇文章主要介紹了基于springboot處理date參數(shù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
詳解IntelliJ IDEA創(chuàng)建spark項(xiàng)目的兩種方式
這篇文章主要介紹了詳解IntelliJ IDEA創(chuàng)建spark項(xiàng)目的兩種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Springboot+MyBatist實(shí)現(xiàn)前后臺(tái)交互登陸功能方式
這篇文章主要介紹了Springboot+MyBatist實(shí)現(xiàn)前后臺(tái)交互登陸功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Java中ByteArrayInputStream和ByteArrayOutputStream用法詳解
這篇文章主要介紹了Java中ByteArrayInputStream和ByteArrayOutputStream用法詳解,?ByteArrayInputStream?的內(nèi)部額外的定義了一個(gè)計(jì)數(shù)器,它被用來(lái)跟蹤?read()?方法要讀取的下一個(gè)字節(jié)2022-06-06

