Java中Base64和File之間互轉代碼示例
更新時間:2023年08月01日 09:50:01 作者:七月七日晴52000
這篇文章主要給大家介紹了關于Java中Base64和File之間互轉的相關資料,Base64是網絡上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個可打印字符來表示二進制數(shù)據的方法,需要的朋友可以參考下
1、Base64 轉 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 轉 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位編碼轉為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ù)據為空
resultMap.put("resultCode", 0);
resultMap.put("msg", "圖片為空");
} else {
BASE64Decoder decoder = new BASE64Decoder();
String baseValue = base64Pic.replaceAll(" ", "+");//前臺在用Ajax傳base64值的時候會把base64中的+換成空格,所以需要替換回來。
byte[] b = decoder.decodeBuffer(baseValue.replace("data:image/jpeg;base64,", ""));//去除base64中無用的部分
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) {// 調整異常數(shù)據
b[i] += 256;
}
}
file = new File(imgFilePath + "\\" + System.currentTimeMillis());
// 如果要返回file文件這邊return就可以了,存到臨時文件中
OutputStream out = new FileOutputStream(file.getPath());
out.write(b);
out.flush();
out.close();
} catch (Exception e) {
resultMap.put("resultCode", 0);
resultMap.put("msg", "存儲異常");
}
}
return file;
}總結
到此這篇關于Java中Base64和File之間互轉的文章就介紹到這了,更多相關Java Base64和File互轉內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中json使用方法_動力節(jié)點Java學院整理
JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據交換格式, json是個非常重要的數(shù)據結構,在web開發(fā)中應用十分廣泛。下面通過本文給大家講解Java中json使用方法,感興趣的朋友一起看看吧2017-07-07
IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)
這篇文章主要介紹了IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝),本文通過截圖給大家展示的非常詳細,需要的朋友可以參考下2021-09-09
Mybatis查詢Sql結果未映射到對應得實體類上的問題解決
使用mybatis查詢表數(shù)據得時候,發(fā)現(xiàn)對應得實體類字段好多都是null,本文主要介紹了Mybatis查詢Sql結果未映射到對應得實體類上的問題解決,具有一定的參考價值,感興趣的可以了解一下2024-02-02
解決Idea查看源代碼警告Library source does not mat
在使用IDEA開發(fā)時,遇到第三方jar包中的源代碼和字節(jié)碼不一致的問題,會導致無法正確打斷點進行調試,這通常是因為jar包更新后源代碼沒有同步更新造成的,解決方法是刪除舊的jar包,通過Maven重新下載或手動下載最新的源代碼包,確保IDE中的源碼與字節(jié)碼版本一致2024-10-10

