Base64與File之間的相互轉(zhuǎn)化方式
Base64與File的相互轉(zhuǎn)化
問題
最近遇到一個上傳文件的問題,前端使用了另一種傳值,就是Base64字符串傳給后臺 ,一開始沒有對其進行解碼操作,存入數(shù)據(jù)庫時就超長了,今天這里提供一種base64和file之間相互轉(zhuǎn)化的工具類,以便日后參考
/**
*
* @param path
* @return String
* @description 將文件轉(zhuǎn)base64字符串
* @date 2018年3月20日
* @author changyl
* File轉(zhuǎn)成編碼成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;
//創(chuàng)建文件目錄
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編碼中會被解碼成空格)將Base64轉(zhuǎn)為文件并保存
public static void base64ToFile(String base64, String fileName, String savePath) {
File file = null;
//創(chuàng)建文件目錄
String filePath = savePath;
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();
}
}
}
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java應(yīng)用開發(fā)之JVM運行時內(nèi)存分析
這篇文章主要介紹了java應(yīng)用開發(fā)之JVM運行時內(nèi)存,文中附含圖文示例內(nèi)容分析非常簡要,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
MyBatis分頁查詢返回list的時候出現(xiàn)null的問題
這篇文章主要介紹了MyBatis分頁查詢返回list的時候出現(xiàn)null的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Springboot打包代碼,反編譯后代碼混淆方式(防止還原代碼)
文章主要介紹了如何對Spring Boot項目進行jar包混淆,以防止反編譯還原原始代碼,通過在項目中添加proguard.cfg文件并配置Maven插件,可以實現(xiàn)代碼混淆,從而增加反編譯的難度2024-11-11
Jmeter參數(shù)化獲取序列數(shù)據(jù)實現(xiàn)過程
這篇文章主要介紹了Jmeter參數(shù)化獲取序列數(shù)據(jù)實現(xiàn)過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
java并發(fā)編程包JUC線程同步CyclicBarrier語法示例
這篇文章主要為大家介紹了java并發(fā)編程工具包JUC線程同步CyclicBarrier語法使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03
在Java中實現(xiàn)讓線程按照自己指定的順序執(zhí)行
這篇文章主要介紹了在Java中實現(xiàn)讓線程按照自己指定的順序執(zhí)行,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
java.io.EOFException: Unexpected end of
本文主要介紹了java.io.EOFException: Unexpected end of ZLIB input stream異常解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05

