Java如何通过Base64对图片文件进行编码和解码操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计421个文字,预计阅读时间需要2分钟。
Base64编码是一种常用的字符编码,广泛应用于数据传输。但它不是安全领域的加密解密算法。Base64主要用于保证数据传输的准确性,而非安全性。
BASE64 编码是一种常用的字符编码,在很多地方都会用到。但base64不是安全领域下的加密解密算法。能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数据的正确性,有些网关或系统只能使用ASCII字符。Base64就是用来将非ASCII字符的数据转换成ASCII字符的一种方法,而且base64特别适合在.....xx.jpg * @return */ public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 ByteArrayOutputStream outputStream = null; try { BufferedImage bufferedImage = ImageIO.read(imageUrl); outputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", outputStream); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串 } /** * 将本地图片进行Base64位编码 * * @param imgUrl * 图片的url路径,如.....xx.jpg * @return */ public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 ByteArrayOutputStream outputStream = null; try { BufferedImage bufferedImage = ImageIO.read(imageFile); outputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", outputStream); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串 } /** * 将Base64位编码的图片进行解码,并保存到指定目录 * * @param base64 * base64编码的图片信息 * @return */ public static void decodeBase64ToImage(String base64, String path, String imgName) { BASE64Decoder decoder = new BASE64Decoder(); try { FileOutputStream write = new FileOutputStream(new File(path + imgName)); byte[] decoderBytes = decoder.decodeBuffer(base64); write.write(decoderBytes); write.close(); } catch (IOException e) { e.printStackTrace(); } } }
2、直接在页面上显示base64编码的图片
<html> <body> <img src='data:image/jpg;base64,base64码'/> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计421个文字,预计阅读时间需要2分钟。
Base64编码是一种常用的字符编码,广泛应用于数据传输。但它不是安全领域的加密解密算法。Base64主要用于保证数据传输的准确性,而非安全性。
BASE64 编码是一种常用的字符编码,在很多地方都会用到。但base64不是安全领域下的加密解密算法。能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数据的正确性,有些网关或系统只能使用ASCII字符。Base64就是用来将非ASCII字符的数据转换成ASCII字符的一种方法,而且base64特别适合在.....xx.jpg * @return */ public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 ByteArrayOutputStream outputStream = null; try { BufferedImage bufferedImage = ImageIO.read(imageUrl); outputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", outputStream); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串 } /** * 将本地图片进行Base64位编码 * * @param imgUrl * 图片的url路径,如.....xx.jpg * @return */ public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 ByteArrayOutputStream outputStream = null; try { BufferedImage bufferedImage = ImageIO.read(imageFile); outputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", outputStream); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串 } /** * 将Base64位编码的图片进行解码,并保存到指定目录 * * @param base64 * base64编码的图片信息 * @return */ public static void decodeBase64ToImage(String base64, String path, String imgName) { BASE64Decoder decoder = new BASE64Decoder(); try { FileOutputStream write = new FileOutputStream(new File(path + imgName)); byte[] decoderBytes = decoder.decodeBuffer(base64); write.write(decoderBytes); write.close(); } catch (IOException e) { e.printStackTrace(); } } }
2、直接在页面上显示base64编码的图片
<html> <body> <img src='data:image/jpg;base64,base64码'/> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

