java
でサーバプログラムから読みながら、私はあいまいな出力を取得しています
を読みながら検出:-1ファイルのjava
byte [] mybytearray = new byte [content_length];
InputStream inputStream = clientSocket0.getInputStream(); //Getting input
from peer0
FileOutputStream fileOutputStream = new FileOutputStream(fileDownloaded);
//Sending the output to local directory
BufferedOutputStream bufferedOutputStream = new
BufferedOutputStream(fileOutputStream);
bytesRead = inputStream.read(mybytearray,0,content_length);
System.out.println("First read : " +bytesRead);
current = bytesRead;
if(bytesRead!=content_length) {
current = bytesRead;
do {
System.out.println(current +"-Current");
System.out.println("Read it : "+(mybytearray.length-current));
bytesRead =
inputStream.read(mybytearray, current, (mybytearray.length-current));
System.out.println("***"+bytesRead);
if(bytesRead == -1) {
//current = content_length;
break;
}
else
current += bytesRead;
} while(current < content_length);
}
bufferedOutputStream.write(mybytearray, 0 , current);
bufferedOutputStream.flush();
bufferedOutputStream.close();
inputStream.close();
inFromServer0.close();
それはいくつかのファイルに対して次のような出力を与えている:
コンテンツ長33996を
C:\ Users \ユーザースミット\ gitの\ IP_Task2 \タスク1 \ピア1/rfc8183.txt.pdf
まず読む:24356
24356-現在
読みますそれ:9640
*** - 1
ループでbytesReadである-1、それによって正しいファイルを作成することはできません。
コードを正しくフォーマットしてください。最近のバージョンのJavaを使用している場合は、 'InputStream.close()'を手動で呼び出すのではなく、try-with-resourceを使用して調べてください。 – omajid