java.lang.IndexOutOfBoundsException:pos< flushedPos!在阅读图像时

时间:2014-02-02 23:25:10

标签: java image png base64

嗨我有这个编码为Base64的String str: iVBORw0KGgoAAAANSUhEUgAAACYAAAAtCAYAAADC+hltAAAABGdBTUEAAK/INwWK6QAAABl0RVh0 U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAA3ZSURBVHjaYvz//z/DYAQAAcREieYpU6YE 6ujoHJSUlFzKy8t7WUpK6riXl9f0+fPnO1HqMIAAYgCFGDk4MjJyAlD/O2ZmZlCQ/2dnZwfTIMzN zX3V1dV19vXr1znINR8ggMjS1NHREQdyFMwhTExMcEdZWFj8DwoK+s/CwvLfzMxsJbkOAwggsjQJ CwvfhjkEhGGhBnKgoqLifzs7u/9CQkIgsfutra1R5NgBEEAka9i3b58KKLS4uLjA0cfBwQF2FCiE 2NjY/iM7GCSvpKS0lxyHAQQQyYn/4sWLRkBK8Nu3bwx//vxh+PnzJwMjIyPYsF+/fjEA0xeDmJgY WC1I7t69e0rHjh2TItUegAAi2WFfv37lBYYMQ3h4OENoaCgDiA1yFDBXMhQXFzPMnj2bQVtbmwEY omD1wGhWePTokRyp9gAEEAupGn79/sumpK7FkJWTziDEw8Fw/v47hpunjzHk5hUxJEaFMTy+/4Ch u6mVwcnNk4GNlRuo/ivDixcvJEi1ByCASA4xJi7ef0bmdgxdHdMZdm09zOClLcrA/f8Lg7aaJENT RxdDUFwmw/NPvxls3a0Zfv3/ygDMEAyfP3/mI9UegAAiOcREJWVeXLpwleHj8zsMu9ecZ0gI+s3A EcfE8OfbdoYnz84yvP/8lOHDl28MjP8EGNiZORl+/vkOSntspNoDEEAkO0xeTPzB/Qu7GapSbBnY vlxhsHa4z+DuocTwm/Udg7iQBEN0oCqDitR9htNHtjD8+ckJzBg/QJmAZIcBBBDJDmP+8Y2F588j BiGGYwymuq8YPv/9ycD79wuDgPQPBheudwxf/7xhuPtiEYOa/CeGD++YGH4B9dy4cUOTVHsAAohk h3Gxsf8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BtQcjC8hDf4CZ5ivDl+/A9Pedew+3tO9qObXw VQyS+h9A+gACiIXv7yfBfyyaN07d+yok9faDPCfzV4bffOzARM/N8BOY81n5eBlYfn1iEGEEFvsf eRj+fAfmPoGfDBeuXmBYuy8PaMQXeGKHFRmwnAkCYuz/GV684WWQlAIWAIxAh/xnZeD4953h7bvn DE8/ipxhUk6bKawZu4RXTB2lagIIIHiH99Gldaav7xx3fHnvtOXn9ze1/v57oSYIrF6lxBgYBLjZ GfiBzZ1fQF9fuPWFYQ+wv7TtOMO9r//kX0mpSN8Bpi9Qf44J6MB/IEcCMwMLsNXKBeyBi+kJP1DI CBFU0pR8zyAtCYzqjwwMz18wMNz7IHPE2LeqXcogcxu2kh8ggLD2xD/fv8F+/fI+zxevruvdubgx nJnhIz8ny09pYKQw3L7/i+Evu8YGc9ekuWEp+VsI1Ujt1dlFzy+s6rXU+MHAxfOd4cNHYHQy66/y iu8rFNKzeoZLH0AAETVEcHzrZsOduxZkfHz/+qqiksk/75DUeco6mt+I6ek/e3ZG6NDGtUHnT+9y /8X2i0PP2P6go2XECgUdmyf49AEEECPd5pK+32diYPvNwsCs9osY5QABBgAA8QC2qQpXrAAAAABJ RU5ErkJggg==

以下是我将其转换为byte[]的方式:

byte[] imageByte = Base64.decode(str);

以下是我想将它保存在我的机器上的方法:

    BufferedImage image = null;
    String path = EVENT_FOLDER + name + "." + extension;

    try
    {
        image = ImageIO.read(new ByteArrayInputStream(imageByte));
        ImageIO.write(image, extension, new File(path));
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }

我得到了这个例外:

java.lang.IndexOutOfBoundsException: pos < flushedPos!
    at javax.imageio.stream.ImageInputStreamImpl.seek(ImageInputStreamImpl.java:768)
    at javax.imageio.stream.ImageInputStreamImpl.skipBytes(ImageInputStreamImpl.java:738)
    at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:676)
    at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1229)
    at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1577)
    at javax.imageio.ImageIO.read(ImageIO.java:1448)
    at javax.imageio.ImageIO.read(ImageIO.java:1352)
    at com.evappserver.util.ImageUploader.uploadEventImage(ImageUploader.java:38)
    at com.evappserver.logiclayer.NewEventLogic.createNewEvent(NewEventLogic.java:64)
    at com.evappserver.servlets.NewEventServlet.doGet(NewEventServlet.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

注意:以下是在线转换器的一些链接:link link link

这个错误说的是什么?我如何解决它?谢谢!

1 个答案:

答案 0 :(得分:1)

我打赌问题是你使用的工具实际上并没有转换原始文件,而是编码图像数据本身。这意味着输入不适合ImageIO