*本事例主要讲了如下几点:
* 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 2 3 4index.html 5 6 7 8 9 10 21 22 48 49