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中的常用注解
- 下一篇: Java中获取近七天的日期(包含今天)