每次从相机捕获图像时,我都会尝试将多张图像从我的 android 手机自动发送到服务器(PC)。
问题是该read()
功能仅在第一次阻塞。因此,从技术上讲,只有一张图像被接收并完美显示。但是之后is.read()
返回时-1
,此函数不会阻塞,并且无法接收多个图像。
服务器的代码很简单
while (true) {
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
is = sock.getInputStream();
if (is != null)
System.out.println("is not null");
int bufferSize = sock.getReceiveBufferSize();
byte[] bytes = new byte[bufferSize];
while ((count = is.read(bytes)) > 0)
{
if (filewritecheck == true)
{
fos = new FileOutputStream("D:\\fypimages\\image" + imgNum + ".jpeg");
bos = new BufferedOutputStream(fos);
imgNum++;
filewritecheck = false;
}
bos.write(bytes, 0, count);
System.out.println("count: " + count);
}
if (count <= 0 && bos != null) {
filewritecheck = true;
bos.flush();
bos.close();
fos.close();
}
}
接收到图像后的输出是
is not null
is not null
is not null
is not null
is not null
is not null
is not null
is not null
...
...
...
...
任何帮助将不胜感激。