我從一個套接字發送文件到另一個套接字。在此之後,我嘗試發送一條簡單消息,但這不起作用。有人可以說明原因嗎?Java Socket信息在文件傳輸後不可讀
發送的文件和消息:
byte[] buffer = new byte[16384];
InputStream inputStream = new FileInputStream(f);
OutputStream outputStream = client.getOutputStream();
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
client.shutdownOutput();
//Following doesnt work:
PrintWriter m_out = new PrintWriter(outputStream);
m_out.println("anfrage erhalten");
m_out.flush();
接收文件和消息:
File pdfFile = new File("marke.pdf");
OutputStream fs = new FileOutputStream(pdfFile);
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
byte[] buffer = new byte[16384];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fs.write(buffer, 0, len);
}
fs.flush();
fs.close();
client.shutdownOutput();
// Here i will receive the Message after the file transfer, but this doesnt work!
System.out.println(br.readLine());
什麼都不起作用,例外?我猜你關閉了'clinet.shutdownOutput()'中的客戶端套接字,但它只是一個猜測... – home
here:System.out.println(br.readLine()); 我應該收到消息「Anfrage erhalten」。但只有NULL和沒有數據。沒有關機是一樣的,這只是一個從任何地方解決我的問題tipp,但它不起作用。如果我發送信息befor我傳輸文件,它的工作原理。 – Fabix123