将图片转成base64字符串并在JSP页面显示的Java代码
*本事例主要讲了如下几点:
* 1:将图片转换为BASE64加密字符串.
* 2:将图片流转换为BASE64加密字符串.
* 3:将BASE64加密字符串转换为图片.
* 4:在jsp文件中以引用的方式和BASE64加密字符串方式展示图片.
首先看工具类:
1 import java.awt.image.BufferedImage; 2 import java.io.ByteArrayInputStream; 3 import java.io.ByteArrayOutputStream; 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.InputStream; 7 8 import javax.imageio.ImageIO; 9 10 import sun.misc.BASE64Decoder; 11 import sun.misc.BASE64Encoder; 12 13 /** 14 * @author IluckySi 15 * @since 20150122 16 */ 17 public class ImageUtil { 18 19 private static BASE64Encoder encoder = new sun.misc.BASE64Encoder(); 20 private static BASE64Decoder decoder = new sun.misc.BASE64Decoder(); 21 22 /** 23 * 将图片转换为BASE64加密字符串. 24 * @param imagePath 图片路径. 25 * @param format 图片格式. 26 * @return 27 */ 28 public String convertImageToByte(String imagePath, String format) { 29 File file = new File(imagePath); 30 BufferedImage bi = null; 31 ByteArrayOutputStream baos = null; 32 String result = null; 33 try { 34 bi = ImageIO.read(file); 35 baos = new ByteArrayOutputStream(); 36 ImageIO.write(bi, format == null ? "jpg" : format, baos); 37 byte[] bytes = baos.toByteArray(); 38 result = encoder.encodeBuffer(bytes).trim(); 39 System.out.println("将图片转换为BASE64加密字符串成功!"); 40 } catch (IOException e) { 41 System.out.println("将图片转换为 BASE64加密字符串失败: " + e); 42 } finally { 43 try { 44 if(baos != null) { 45 baos.close(); 46 baos = null; 47 } 48 } catch (Exception e) { 49 System.out.println("关闭文件流发生异常: " + e); 50 } 51 } 52 return result; 53 } 54 55 /** 56 * 将图片流转换为BASE64加密字符串. 57 * @param imageInputStream 58 * @param format 图片格式. 59 * @return 60 */ 61 public String convertImageStreamToByte(InputStream imageInputStream, String format) { 62 BufferedImage bi = null; 63 ByteArrayOutputStream baos = null; 64 String result = null; 65 try { 66 bi = ImageIO.read(imageInputStream); 67 baos = new ByteArrayOutputStream(); 68 ImageIO.write(bi, format == null ? "jpg" : format, baos); 69 byte[] bytes = baos.toByteArray(); 70 result = encoder.encodeBuffer(bytes).trim(); 71 System.out.println("将图片流转换为BASE64加密字符串成功!"); 72 } catch (IOException e) { 73 System.out.println("将图片流转换为 BASE64加密字符串失败: " + e); 74 } finally { 75 try { 76 if(baos != null) { 77 baos.close(); 78 baos = null; 79 } 80 } catch (Exception e) { 81 System.out.println("关闭文件流发生异常: " + e); 82 } 83 } 84 return result; 85 } 86 87 /** 88 * 将BASE64加密字符串转换为图片. 89 * @param base64String 90 * @param imagePath 图片生成路径. 91 * @param format 图片格式. 92 */ 93 public void convertByteToImage(String base64String, String imagePath, String format) { 94 byte[] bytes = null; 95 ByteArrayInputStream bais = null; 96 BufferedImage bi = null; 97 File file = null; 98 try { 99 bytes = decoder.decodeBuffer(base64String); 100 bais = new ByteArrayInputStream(bytes); 101 bi = ImageIO.read(bais); 102 file = new File(imagePath); 103 ImageIO.write(bi, format == null ? "jpg" : format, file); 104 System.out.println("将BASE64加密字符串转换为图片成功!"); 105 } catch (IOException e) { 106 System.out.println("将BASE64加密字符串转换为图片失败: " + e); 107 } finally { 108 try { 109 if(bais != null) { 110 bais.close(); 111 bais = null; 112 } 113 } catch (Exception e) { 114 System.out.println("关闭文件流发生异常: " + e); 115 } 116 } 117 } 118 }
然后看测试类:
1 import java.io.InputStream; 2 3 import com.ilucky.util.image.ImageUtil; 4 5 /** 6 * 本事例主要讲了如下几点: 7 * 1:将图片转换为BASE64加密字符串. 8 * 2:将图片流转换为BASE64加密字符串. 9 * 3:将BASE64加密字符串转换为图片. 10 * 4:在jsp文件中以引用的方式和BASE64加密字符串方式展示图片. 11 * @author IluckySi 12 * @since 20150122 13 */ 14 public class MainTest { 15 16 public static void main(String[] args) { 17 ImageUtil imageUtil = new ImageUtil(); 18 19 String base64String = imageUtil.convertImageToByte("D:\\test.png", "png"); 20 System.out.println(base64String); 21 imageUtil.convertByteToImage(base64String, "D:\\test2.png", "png"); 22 23 InputStream is = MainTest.class.getResourceAsStream("test.png"); 24 String base64String2 = imageUtil.convertImageStreamToByte(is, "png"); 25 System.out.println(base64String2); 26 imageUtil.convertByteToImage(base64String2, "D:\\test3.png", "png"); 27 } 28 }
最后看jsp文件:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>index.html</title> 5 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 6 <meta http-equiv="description" content="this is my page"> 7 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 8 </head> 9 <body> 10 <!-- 11 data表示取得数据的协定名称,image/png是数据类型名称,base64 是数据的编码方法, 12 逗号后面是image/png文件base64编码后的数据. 13 data:text/css;base64,base64编码的CSS代码 14 data:text/javascript,Javascript代码 15 data:text/javascript;base64,base64编码的Javascript代码 16 data:image/gif;base64,base64编码的gif图片数据 17 data:image/png;base64,base64编码的png图片数据 18 data:image/jpeg;base64,base64编码的jpeg图片数据 19 data:image/x-icon;base64,base64编码的icon图片数据 20 --> 21 <img src="test.png"/> 22 <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAFYklEQVR42sVXfUyUdRz/8XKhMZQO 23 7nmeuwMFwrBEVJqgDOcUHZvNuTarrdKam0zdKrckAy4goowxbaM5hE1yjVVzLjLRokBFz2qsQPGF 24 F8vmamtkL4s4OHDy6ff5EQcXx91hf/jHB777fj8v3+d57p7nOQFA3EuoPxcLhV90SlxziOSeUrHd 25 WRz5fv1uc1fl03OGizdGgmBd/5K5mzNyyO0M4EkEXqBAiI58Ed9WEFp1+MXYm1vW68hKM7AwJR5W 26 mx3RZqsCa/Y4I4dcaqilx10t0PGqCOssEjuvH7H9vW1zEgzNgK5ZYVh02HUN86w65tvGwJo9zhRH 27 cqmhlh70mtEC7a8Ie2ep6dPfW5eM3OnbhMo9S6GbLV6h04Eccqmhlh70omdQC3TsFbbLZaYL/W2Z 28 wA+PYbQ9B780rUJu1nxoMZaAC5BDLjXU0oNe9K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Nx4IwrDVZFwveMbnJFD7uSzQC960psZft8Je4rFkzXPzh5eME+HTTfUs94mbxx1W+cCVbMw 43 uH82XNOAM3LIpUZppQe96EnvoN6K5TXaVfPMbCxO1BAVbeCJzBj89tYsDO+/DwOVEX5BDrmbpYZa 44 etCLnkG/ll8qEqK7WOQe32lybs54AKd23I/RA+EYqDAFBXJPSg219KAXPWf8y+iKQxhXHaLyRklI 45 363yUAzsC8OghGtfqE9wRg651FBLj7v/aTb+ul4oUi4VipIuh+i+WSKG+spC8Gd5CPr/BWv2OJOc 46 Hsl9XWoW/v/fht5LhLUXiARZb71cJOquFIlv5BH2EqzZ40xyEsm9OJMfp/cS/wC6xRqbyM2GFQAA 47 AABJRU5ErkJggg=="/> 48 </body> 49 </html>
不定期会发布一些实用的Java开发文章