简体   繁体   English

ImageIO什么时候应该停止从InputStream读取?

[英]When should ImageIO stop reading from InputStream?

I am trying make a server that receives a JPG image over the newtork in Java. 我正在尝试使服务器通过Java中的newtork接收JPG图像。

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. 仅当我关闭连接时ImageIO才会停止。 It also stops when I send the same image twice. 当我发送相同的图像两次时,它也会停止。

ImageIO.read should continue reading from the input stream until it has the entire file, at which point it stops reading. ImageIO.read应该继续从输入流中读取,直到拥有整个文件为止,此时它将停止读取。 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. 据我所知,ImageIO.read无法读取两个图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM