首页 > 编程知识 正文

JavainputStream转换成Base64字符串

时间:2023-05-03 11:57:05 阅读:284531 作者:3636

/** * 将inputstream转为Base64 * * @param is * @return * @throws Exception */ private String getBase64FromInputStream(InputStream is) throws Exception { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 byte[] data = null; // 读取图片字节数组 try { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = is.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } data = swapStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { throw new Exception("输入流关闭异常"); } } } return Base64.byteArrayToBase64(data); }

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