ImageIO什么时候应该停止从InputStream读取?
我正在尝试制作一个通过 Java 中的 newtork 接收 JPG 图像的服务器。
isr = socket.getInputStream();
BufferedImage img = ImageIO.read(isr);
我想读完,但保持套接字连接。仅当我关闭连接时 ImageIO 才会停止。当我发送同一张图像两次时它也会停止。
I am trying make a server that receives a JPG image over the newtork in Java.
isr = socket.getInputStream();
BufferedImage img = ImageIO.read(isr);
I would like to finish reading, but keep the socket connection. ImageIO stops only when I close the connection. It also stops when I send the same image twice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ImageIO.read 应继续从输入流读取,直到拥有整个文件,此时停止读取。然后,调用者有责任在输入流上调用 close。
您不会从客户端向服务器发送任何数据来告诉服务器您已完成阅读。只需关闭连接即可。
如果服务器发送相同的图像两次,或发送两个不同的图像,则必须建立两个套接字连接,或使用其他方法读取数据。据我所知,ImageIO.read 无法读取两个图像。
ImageIO.read should continue reading from the input stream until it has the entire file, at which point it stops reading. It is then teh responsibility of the caller to call close on the input stream.
You don't send any data from the client to the server to tell it you are done reading. Just close the connection.
If the server sends the same image twice, or sends two different images, you must make two socket connections, or use some other method to read the data. To the best of my knowledge, there's no way for ImageIO.read to read two images.