public static void main(String[] args) { // 测试1:将图片转换成base64编码字符串 String code = imgToCode("D:\\tupain.jpg"); System.out.println("生成的base64编码字符串是:" + code); // 测试2:将base64编码字符串变成图片 boolean flag = codeToImg(code, "D:\\tupian2.jpg"); System.out.println("是否成功生成图片:" + (flag ? "是" : "否")); } /** * 将图片转换为base64编码字符串 * @param imgDic 图片的全路径 * @return 图片的base64编码结果 */ private static String imgToCode(String imgDic) { // 参数验证 File file = new File(imgDic); if (!(!StringUtils.isEmpty(imgDic) && file.exists() && file.isFile())) { throw new RuntimeException("参数不符合要求"); } // 将图片转换成字节数组 InputStream inputStream = null; byte[] bytes = null; try { inputStream = new FileInputStream(imgDic); bytes = new byte[inputStream.available()]; inputStream.read(bytes); } catch (Exception e) { throw new RuntimeException("转换过程中出现异常"); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 生成base64编码字符串 byte[] result = Base64.encodeBase64(bytes); return new String(result); } /** * 根据图片的base64编码字符串生成图片到指定位置 * @param imgCode 图片的base64编码字符串 * @param imgDir 生成图片的指定位置的全路径 * @return */ private static boolean codeToImg(String imgCode, String imgDir) { // 参数验证 if (StringUtils.isEmpty(imgCode)) { throw new RuntimeException("参数不符合要求"); } OutputStream outputStream = null; try { // 对Base64解码字符串进行解码 byte[] bytes = Base64.decodeBase64(imgCode); // 处理异常数据 for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) { bytes[i] += 256; } } // 生成图片到指定位置 outputStream = new FileOutputStream(imgDir); outputStream.write(bytes); outputStream.flush(); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; }
注意
当图片转换为base64编码字符串后,其中包含大量的+
号,如果我们将上述base64编码字符串通过网络传输给其他接口,那么服务器在解析数据时会把+
号当成连接符,然后自动将+
号转换为空格,所以为保证数据的准确性,我们需要将空格转换成+
号,转换方法如下:
String code = code.replace(" ", "+");
然后获得的code
就和传输之前的一样了
base64字符串如何在img标签的src属性上使用
写法如下:
<img src="data:image/png;base64,后端返回的base64字符串" />