Java中读取本地图片并转为base64解决办法
生活随笔
收集整理的這篇文章主要介紹了
Java中读取本地图片并转为base64解决办法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/**
* imgFile 圖片本地存儲路徑
*/
public static String getImgFileToBase64(String imgFile) {//將圖片文件轉化為字節數組字符串,并對其進行Base64編碼處理InputStream inputStream = null;byte[] buffer = null;//讀取圖片字節數組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 {// 關閉inputStream流inputStream.close();} catch (IOException e) {e.printStackTrace();}}}// 對字節數組Base64編碼return new BASE64Encoder().encode(buffer);}
總結
以上是生活随笔為你收集整理的Java中读取本地图片并转为base64解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: easyexcel中的常用注解
- 下一篇: git commit 提交的时候报错hu