Java中读取本地图片并转为base64解决办法
生活随笔
收集整理的這篇文章主要介紹了
Java中读取本地图片并转为base64解决办法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/**
* imgFile 圖片本地存儲(chǔ)路徑
*/
public static String getImgFileToBase64(String imgFile) {//將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理InputStream inputStream = null;byte[] buffer = null;//讀取圖片字節(jié)數(shù)組try {inputStream = new FileInputStream(imgFile);int count = 0;while (count == 0) {count = inputStream.available();}buffer = new byte[count];inputStream.read(buffer);} catch (IOException e) {e.printStackTrace();} finally {if (inputStream != null) {try {// 關(guān)閉inputStream流inputStream.close();} catch (IOException e) {e.printStackTrace();}}}// 對(duì)字節(jié)數(shù)組Base64編碼return new BASE64Encoder().encode(buffer);}
總結(jié)
以上是生活随笔為你收集整理的Java中读取本地图片并转为base64解决办法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: easyexcel中的常用注解
- 下一篇: Java中获取近七天的日期(包含今天)