ソケット接続で複数のファイルを送信したい。1 つのファイルの場合は完全に機能しますが、複数 (一度に 1 つずつ) 送信しようとすると、次のようになりますSocket Exception
。
java.net.SocketException: ソケットが閉じられました
一般的に、私の接続は次のように機能します。
- サーバーは接続を待機しています
- クライアントはサーバーに接続し、特定のファイル (ファイル名を含む文字列) の要求を送信します。
- サーバーはローカルファイルを読み取り、クライアントに送信します
- クライアントは別のファイルに対する別の要求を送信し、ポイント 3 に進みます。
リクエスト待ちプロシージャの実行メソッドは次のようになります。
@Override
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
if (message.equals(REQUESTKEY)) {
System.out.println("read files from directory and send back");
sendStringToClient(createCodedDirContent(getFilesInDir(new File(DIR))), socket);
} else if (message.startsWith(FILE_PREFIX)) {
String filename = message.substring(FILE_PREFIX.length());
try {
sendFile(new File(DIR + filename));
} catch (IOException e) {
System.err.println("Error: Could not send File");
e.printStackTrace();
}
} else {
System.out.println("Key unknown!");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
私のsendFile()
メソッドは次のようになります。
public void sendFile(File file) throws IOException {
FileInputStream input = new FileInputStream(file);
OutputStream socketOut = socket.getOutputStream();
System.out.println(file.getAbsolutePath());
int read = 0;
while ((read = input.read()) != -1) {
socketOut.write(read);
}
socketOut.flush();
System.out.println("File successfully sent!");
input.close();
socketOut.close();
}
問題は にあると思いますsocketOut.close()
。残念ながら、このメソッドはソケット接続も閉じます (以降の接続の問題)。しかし、このクローズを省略した場合、ファイル転送が正しく機能しません。クライアントに不完全なファイルが到着します。
この問題を回避または修正するにはどうすればよいですか? または、要求された複数のファイルを転送するより良い方法はありますか?
ありがとうございました