首页 > 编程知识 正文

Java中读取本地片并转为base64解决办法

时间:2023-05-06 18:50:21 阅读:284535 作者:2105

/*** 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); }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。