Java實(shí)現(xiàn)base64圖片編碼數(shù)據(jù)轉(zhuǎn)換為本地圖片的方法
本文實(shí)例講述了Java實(shí)現(xiàn)base64圖片編碼數(shù)據(jù)轉(zhuǎn)換為本地圖片的方法。分享給大家供大家參考,具體如下:
項(xiàng)目中用到的把base64圖片數(shù)據(jù)轉(zhuǎn)為本地圖片的函數(shù)
/** * 替換html中的base64圖片數(shù)據(jù)為實(shí)際圖片 * @param html * @param fileRoot 本地路徑 * @param serRoot 服務(wù)器路徑 * @return */ public static String replaceBase64Image(String html,String fileRoot,String serRoot){ File file = new File(fileRoot); if(!file.exists()){//文件根目錄不存在時(shí)創(chuàng)建 new File(fileRoot).mkdirs(); } String htmlContent = html; Pattern pattern = Pattern.compile("\\<img[^>]*src=\"data:image/[^>]*>"); Matcher matcher = pattern.matcher(html); GUIDUtils.init(); while(matcher.find()){ //找出base64圖片元素 String str = matcher.group(); String src = ExStringUtils.substringBetween(str, "src=\"", "\"");//src="..." String ext = ExStringUtils.defaultIfEmpty(ExStringUtils.substringBetween(str, "data:image/", ";"), "jpg");//圖片后綴 String base64ImgData = ExStringUtils.substringBetween(str, "base64,", "\"");//圖片數(shù)據(jù) if(ExStringUtils.isNotBlank(ext)&&ExStringUtils.isNotBlank(base64ImgData)){ //data:image/gif;base64,base64編碼的gif圖片數(shù)據(jù) //data:image/png;base64,base64編碼的png圖片數(shù)據(jù) if("jpeg".equalsIgnoreCase(ext)){//data:image/jpeg;base64,base64編碼的jpeg圖片數(shù)據(jù) ext = "jpg"; } else if("x-icon".equalsIgnoreCase(ext)){//data:image/x-icon;base64,base64編碼的icon圖片數(shù)據(jù) ext = "ico"; } String fileName = GUIDUtils.buildMd5GUID(false)+"."+ext;//待存儲(chǔ)的文件名 String filePath = fileRoot+File.separator+fileName;//圖片路徑 try { convertBase64DataToImage(base64ImgData, filePath);//轉(zhuǎn)成文件 String serPath = serRoot+fileName;//服務(wù)器地址 htmlContent = htmlContent.replace(src, serPath);//替換src為服務(wù)器地址 } catch (IOException e) { e.printStackTrace(); } } } return htmlContent; } /** * 把base64圖片數(shù)據(jù)轉(zhuǎn)為本地圖片 * @param base64ImgData * @param filePath * @throws IOException */ public static void convertBase64DataToImage(String base64ImgData,String filePath) throws IOException { BASE64Decoder d = new BASE64Decoder(); byte[] bs = d.decodeBuffer(base64ImgData); FileOutputStream os = new FileOutputStream(filePath); os.write(bs); os.close(); }
PS:這里再為大家提供幾款base64在線工具供大家參考:
BASE64編碼解碼工具:
http://tools.jb51.net/transcoding/base64
在線圖片轉(zhuǎn)換BASE64工具:
http://tools.jb51.net/transcoding/img2base64
Base64在線編碼解碼 UTF-8版:
http://tools.jb51.net/tools/base64_decode-utf8.php
Base64在線編碼解碼 gb2312版:
http://tools.jb51.net/tools/base64_decode-gb2312.php
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java編碼操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java通過(guò)Freemarker模板實(shí)現(xiàn)生成Word文件
FreeMarker是一款模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來(lái)生成輸出文本的通用工具。本文將根據(jù)Freemarker模板實(shí)現(xiàn)生成Word文件,需要的可以參考一下2022-09-09Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Java使用Condition控制線程通信的方法實(shí)例詳解
這篇文章主要介紹了Java使用Condition控制線程通信的方法,結(jié)合實(shí)例形式分析了使用Condition類同步檢測(cè)控制線程通信的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09MySQL中drop、truncate和delete的區(qū)別小結(jié)
在MySQL數(shù)據(jù)庫(kù)管理中,常常需要執(zhí)行刪除數(shù)據(jù)的操作,本文主要介紹了MySQL中drop、truncate和delete的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04關(guān)于ArrayList初始化容量的問(wèn)題
這篇文章主要介紹了關(guān)于ArrayList初始化容量的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03利用Spring Social輕松搞定微信授權(quán)登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權(quán)登錄的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12SpringBoot整合MD5加密完成注冊(cè)和登錄方式
MD5(MessageDigestAlgorithm5)是一種常見的哈希算法,用于生成固定長(zhǎng)度(128位)的哈希值,主要應(yīng)用于數(shù)據(jù)完整性校驗(yàn)和密碼存儲(chǔ),MD5具有快速計(jì)算、不可逆性和抗碰撞性等特點(diǎn),盡管存在碰撞漏洞,MD5仍廣泛應(yīng)用于文件下載校驗(yàn)和數(shù)字簽名等場(chǎng)景2024-10-10