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)容感興趣的讀者可查看本站專(zhuān)題:《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)文章
實(shí)現(xiàn)一個(gè)基于Servlet的hello world程序詳解步驟
Java Servlet 是運(yùn)行在 Web 服務(wù)器或應(yīng)用服務(wù)器上的程序,它是作為來(lái)自 Web 瀏覽器或其他 HTTP 客戶(hù)端的請(qǐng)求和 HTTP 服務(wù)器上的數(shù)據(jù)庫(kù)或應(yīng)用程序之間的中間層2022-02-02
解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問(wèn)題
最近在創(chuàng)建了SpringBoot項(xiàng)目后往配置文件中寫(xiě)了相關(guān)的系統(tǒng)配置,并且在上面加了中文注釋?zhuān)窃谥貑㈨?xiàng)目或開(kāi)機(jī)重啟后遇到了注釋亂碼的情況,下面這篇文章主要給大家介紹一下如何解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問(wèn)題,需要的朋友可以參考下2023-06-06
spring boot 常見(jiàn)http請(qǐng)求url參數(shù)獲取方法
這篇文章主要介紹了spring boot 常見(jiàn)http請(qǐng)求url參數(shù)獲取,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Spring cloud Feign 深度學(xué)習(xí)與應(yīng)用詳解
這篇文章主要介紹了Spring cloud Feign 深度學(xué)習(xí)與應(yīng)用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
springboot實(shí)現(xiàn)敏感字段加密存儲(chǔ)解密顯示功能
這篇文章主要介紹了springboot實(shí)現(xiàn)敏感字段加密存儲(chǔ),解密顯示,通過(guò)mybatis,自定義注解+AOP切面,Base64加解密方式實(shí)現(xiàn)功能,本文通過(guò)代碼實(shí)現(xiàn)給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02

