将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片
data格式的Url最直接的好处是,这些Url原本会引起一个新的网络访问,因为那里是一个网页的地址,现在不会有新的网络访问了,因为现在这里是网页的内容。这样做,会减少服务器的负载,当然同时也增加了当前网页的大小。所以对“小”数据特别有好处。
另外听说这种方式可以将一些敏感的图片放进网页中...挺好玩的.....
一、 在网页中查看图片的方法为:
<body> <span><img width=100 height=100 src="data:image/gif;base64,(此处填图片转成base64编码后的编码)"/></span> </body>
由于base64编码太占用篇幅因此仅仅用括号说明了一下。
二、用Java将图片编码的代码,与将base64的编码转成图片的Java代码:
package base64; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class TestImageBinary { static BASE64Encoder encoder = new sun.misc.BASE64Encoder(); static BASE64Decoder decoder = new sun.misc.BASE64Decoder(); public static void main(String[] args) { System.out.println(getImageBinary()); //将图片转成base64编码 base64StringToImage(getImageBinary()); //将base64的编码转成图片 } static String getImageBinary(){ File f = new File("d://1.jpg"); BufferedImage bi; try { bi = ImageIO.read(f); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", baos); byte[] bytes = baos.toByteArray(); return encoder.encodeBuffer(bytes).trim(); } catch (IOException e) { e.printStackTrace(); } return null; } static void base64StringToImage(String base64String){ try { byte[] bytes1 = decoder.decodeBuffer(base64String); ByteArrayInputStream bais = new ByteArrayInputStream(bytes1); BufferedImage bi1 =ImageIO.read(bais); File w2 = new File("d://2.png");//可以是jpg,png,gif格式 ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动 } catch (IOException e) { e.printStackTrace(); } } }
此Java代码sun.misc.BASE64Decoder这个包导入不进去的话,请将eclipse的Java运行环境修改成不是eclipse自带的jdk,我本来导入不进去,换成本地1.7的jdk就OK了。
一下提供一个编码后图片的url:
<span><img width=10 height=10 src="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