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)文章
詳解多云架構(gòu)下的JAVA微服務(wù)技術(shù)解析
本文介紹了基于開(kāi)源自建和適配云廠商開(kāi)發(fā)框架兩種構(gòu)建多云架構(gòu)的思路,以及這些思路的優(yōu)缺點(diǎn)2021-05-05Java中json使用方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式, json是個(gè)非常重要的數(shù)據(jù)結(jié)構(gòu),在web開(kāi)發(fā)中應(yīng)用十分廣泛。下面通過(guò)本文給大家講解Java中json使用方法,感興趣的朋友一起看看吧2017-07-07IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)
這篇文章主要介紹了IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝),本文通過(guò)截圖給大家展示的非常詳細(xì),需要的朋友可以參考下2021-09-09Mybatis查詢Sql結(jié)果未映射到對(duì)應(yīng)得實(shí)體類上的問(wèn)題解決
使用mybatis查詢表數(shù)據(jù)得時(shí)候,發(fā)現(xiàn)對(duì)應(yīng)得實(shí)體類字段好多都是null,本文主要介紹了Mybatis查詢Sql結(jié)果未映射到對(duì)應(yīng)得實(shí)體類上的問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02解決Idea查看源代碼警告Library source does not mat
在使用IDEA開(kāi)發(fā)時(shí),遇到第三方j(luò)ar包中的源代碼和字節(jié)碼不一致的問(wèn)題,會(huì)導(dǎo)致無(wú)法正確打斷點(diǎn)進(jìn)行調(diào)試,這通常是因?yàn)閖ar包更新后源代碼沒(méi)有同步更新造成的,解決方法是刪除舊的jar包,通過(guò)Maven重新下載或手動(dòng)下載最新的源代碼包,確保IDE中的源碼與字節(jié)碼版本一致2024-10-10JAVA基礎(chǔ)--如何通過(guò)異常處理錯(cuò)誤
這篇文章主要介紹了JAVA中如何通過(guò)異常處理錯(cuò)誤,文中講解非常細(xì)致,代碼幫助大家更好的理解,感興趣的朋友可以了解下2020-06-06