Base64與File之間的相互轉(zhuǎn)化方式
Base64與File的相互轉(zhuǎn)化
問題
最近遇到一個(gè)上傳文件的問題,前端使用了另一種傳值,就是Base64字符串傳給后臺 ,一開始沒有對其進(jìn)行解碼操作,存入數(shù)據(jù)庫時(shí)就超長了,今天這里提供一種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();
}
}
}
}需要注意
標(biāo)紅的base64在這里需要去掉
baseStr = baseStr.replace("data:image/jpeg;base64,", "");//base64解密部分亂碼問題(“+” 號,在urlecode編碼中會(huì)被解碼成空格)將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();
}
}
}
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中的ImportBeanDefinitionRegistrar接口詳解
這篇文章主要介紹了Spring中的ImportBeanDefinitionRegistrar接口詳解,ImportBeanDefinitionRegistrar接口是也是spring的擴(kuò)展點(diǎn)之一,它可以支持我們自己寫的代碼封裝成BeanDefinition對象,注冊到Spring容器中,功能類似于注解@Service @Component,需要的朋友可以參考下2023-09-09
使用@Validated和@Valid 解決list校驗(yàn)的問題
這篇文章主要介紹了使用@Validated和@Valid 解決list校驗(yàn)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
教你利用springboot集成swagger并生成接口文檔
有很多小伙伴不會(huì)利用springboot集成swagger并生成接口文檔,今天特地整理了這篇文章,文中有非常詳細(xì)的代碼圖文介紹及代碼示例,對不會(huì)這個(gè)方法的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Spring-基于Spring使用自定義注解及Aspect實(shí)現(xiàn)數(shù)據(jù)庫切換操作
這篇文章主要介紹了Spring-基于Spring使用自定義注解及Aspect實(shí)現(xiàn)數(shù)據(jù)庫切換操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

