我想從android手機(客戶端)到PC(服務器)之間發送兩個差異爲5秒的圖像。 我正在使用InputStream爲我做這個。InputStream明確接收兩次文件
ServerSocket servsock = new ServerSocket(27508);
Socket sock = servsock.accept();
System.out.println("connection accepted ");
int count;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
InputStream is = null;
is = sock.getInputStream();
int bufferSize = sock.getReceiveBufferSize();
byte[] bytes = new byte[bufferSize];
System.out.println("Here1");
fos = new FileOutputStream("D:\\fypimages\\image" + imgNum + ".jpeg");
bos = new BufferedOutputStream(fos);
imgNum++;
while ((count = is.read(bytes)) > 0)
{
bos.write(bytes, 0, count);
System.out.println("count: " + count);
}
bos.flush();
bytes = new byte[bufferSize];
System.out.println("Here2");
fos = new FileOutputStream("D:\\fypimages\\image" + imgNum + ".jpeg");
bos = new BufferedOutputStream(fos);
imgNum++;
while ((count = is.read(bytes)) > 0)
{
bos.write(bytes, 0, count);
System.out.println("count: " + count);
}
bos.flush();
System.out.println("Here3");
問題是is.read(bytes)
塊只對所述第一圖像的代碼,然後結束程序,它不用於第二圖像塊。
我知道它返回-1
當第一個圖像完全收到,但我如何使它第二次工作?
這只是您自己以前的問題的轉發[使用InputStream通過TCP套接字接收多個圖像](http://stackoverflow.com/questions/16189128/recieving-multiple-images-over-tcp-socket-using- inputstream) – EJP 2013-04-28 22:52:20