JAVA實(shí)現(xiàn)網(wǎng)絡(luò)/本地圖片轉(zhuǎn)BASE64存儲(chǔ)代碼示例
網(wǎng)絡(luò)圖片轉(zhuǎn)BASE64
String encoder = "data:image/jpg;base64,"; //定義圖片類型,方便前端直接使用 ByteArrayOutputStream data = new ByteArrayOutputStream(); URL url = new URL(picUrl);//picUrl為圖片地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream is = connection.getInputStream(); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1){ data.write(bytes,0,len); } is.close(); BASE64Encoder base64Encoder = new BASE64Encoder(); encoder = encoder + base64Encoder.encode(data.toByteArray()).replace("\r\n","").trim();//這里去掉結(jié)果里面的"\r\n",也可以不去,但是不去的話需要使用的時(shí)候還是要去掉,所以為了方便就先去掉再存儲(chǔ)
如果是本地圖片的話,其實(shí)和網(wǎng)絡(luò)圖片相差不多的,主要就是讀取圖片流的形式變一下
String encoder = "data:image/jpg;base64,"; //定義圖片類型,方便前端直接使用 ByteArrayOutputStream data = new ByteArrayOutputStream(); String filePath = "filePath";//這里的filePath為本地存放圖片的地址 FileInputStream is = new FileInputStream(filePath); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1){ data.write(bytes,0,len); } is.close(); BASE64Encoder base64Encoder = new BASE64Encoder(); encoder = encoder + base64Encoder.encode(data.toByteArray()).replace("\r\n","").trim();//這里去掉結(jié)果里面的"\r\n",也可以不去,但是不去的話需要使用的時(shí)候還是要去掉,所以為了方便就先去掉再存儲(chǔ)
附:Java實(shí)現(xiàn)圖片和base64之間的互轉(zhuǎn)
import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Base64; public class ImageUtil { /** * 圖片轉(zhuǎn)Base64碼 * @param src * @return */ public static String convertImageToBase64Str(String src) { ByteArrayOutputStream baos = null; try { String suffix = src.substring(src.lastIndexOf(".") + 1); File imageFile = new File(src); BufferedImage bufferedImage = ImageIO.read(imageFile); baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, suffix, baos); byte[] bytes = baos.toByteArray(); return Base64.getEncoder().encodeToString(bytes); } catch (Exception e) { e.printStackTrace(); } finally { try { if (baos != null) { baos.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } /** * Base64碼轉(zhuǎn)圖片 * @param base64String * @param newSrc */ public static void convertBase64StrToImage(String base64String, String newSrc) { ByteArrayInputStream bais = null; try { String suffix = newSrc.substring(newSrc.lastIndexOf(".") + 1); byte[] bytes = Base64.getDecoder().decode(base64String); bais = new ByteArrayInputStream(bytes); BufferedImage bufferedImage = ImageIO.read(bais); File imageFile = new File(newSrc); ImageIO.write(bufferedImage, suffix, imageFile); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bais != null) { bais.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
總結(jié)
到此這篇關(guān)于JAVA實(shí)現(xiàn)網(wǎng)絡(luò)/本地圖片轉(zhuǎn)BASE64存儲(chǔ)的文章就介紹到這了,更多相關(guān)JAVA圖片轉(zhuǎn)BASE64存儲(chǔ)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?精煉解讀時(shí)間復(fù)雜度與空間復(fù)雜度
對(duì)于一個(gè)算法,其時(shí)間復(fù)雜度和空間復(fù)雜度往往是相互影響的,當(dāng)追求一個(gè)較好的時(shí)間復(fù)雜度時(shí),可能會(huì)使空間復(fù)雜度的性能變差,即可能導(dǎo)致占用較多的存儲(chǔ)空間,這篇文章主要給大家介紹了關(guān)于Java時(shí)間復(fù)雜度、空間復(fù)雜度的相關(guān)資料,需要的朋友可以參考下2022-03-03SpringBoot使用jsr303校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot使用jsr303校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10springboot項(xiàng)目Redis統(tǒng)計(jì)在線用戶的實(shí)現(xiàn)示例
最近做個(gè)項(xiàng)目需要統(tǒng)計(jì)在線用戶,本文主要介紹了springboot項(xiàng)目Redis統(tǒng)計(jì)在線用戶的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06Java通俗易懂系列設(shè)計(jì)模式之責(zé)任鏈模式
這篇文章主要介紹了Java通俗易懂系列設(shè)計(jì)模式之責(zé)任鏈模式,對(duì)設(shè)計(jì)模式感興趣的同學(xué),一定要看一下2021-04-04IKAnalyzer結(jié)合Lucene實(shí)現(xiàn)中文分詞(示例講解)
下面小編就為大家?guī)硪黄狪KAnalyzer結(jié)合Lucene實(shí)現(xiàn)中文分詞(示例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10java使用FastJson解析Json數(shù)據(jù)
本篇文章主要介紹了java使用FastJson解析Json數(shù)據(jù),fastjson 是一個(gè)性能極好的用 Java 語言實(shí)現(xiàn)的 JSON 解析器和生成器,有興趣的可以了解一下。2017-02-02