ImageIO.read(InputStream)的问题
BufferedImage image=null;
InputStream in=B2BSend.regAcc();//得到一个图片的Input流
try {
ImageInputStream imagein= ImageIO.createImageInputStream(in);
image=ImageIO.read(imagein);//报错
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
像代码里说的那样,我想把Input流直接保存成一个BufferedImage对象,这样就不用把Input写到图片文件里,然后再用ImageIO.read将图片显示了。但是read的时候报错了
报错如下:
javax.imageio.IIOException: Error reading PNG image data
at com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at com.frame.VerifyCode.<init>(VerifyCode.java:115)
at com.frame.VerifyCode$1.run(VerifyCode.java:45)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:130)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:127)
at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:161)
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:159)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at javax.imageio.stream.FileCacheImageInputStream.readUntil(Unknown Source)
at javax.imageio.stream.FileCacheImageInputStream.read(Unknown Source)
at com.sun.imageio.plugins.common.SubImageInputStream.read(Unknown Source)
at com.sun.imageio.plugins.common.InputStreamAdapter.read(Unknown Source)
at java.io.SequenceInputStream.read(Unknown Source)
at java.util.zip.InflaterInputStream.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readFully(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(Unknown Source)
... 14 more
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.frame.VerifyCode.<init>(VerifyCode.java:121)
at com.frame.VerifyCode$1.run(VerifyCode.java:45)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)