[英]The server is sending data through the socket, but the client is not receving it (Java)
我正在使用Java編寫文件存儲和傳輸系統。 這是客戶端上接收文件的代碼:
public static void receiveFile(Socket socket) throws IOException{
String fileLocation="/home/limafoxtrottango/Downloads/receivedFile";
int bytesRead=0;
int current = 0;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
// receive file
byte [] byteArray = new byte [60022386];
System.out.println("Waiting to receive a file...");
//reading file from socket
InputStream inputStream = socket.getInputStream();
fileOutputStream = new FileOutputStream(fileLocation);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bytesRead = inputStream.read(byteArray,0,byteArray.length); //copying file from socket to byteArray
current = bytesRead;
do {
bytesRead =inputStream.read(byteArray, current, (byteArray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bufferedOutputStream.write(byteArray, 0 , current); //writing byteArray to file
bufferedOutputStream.flush(); //flushing buffers
System.out.println("File " + fileLocation + " downloaded ( size: " + current + " bytes read)");
} catch(SocketException e){
System.out.println("Some error occured");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (fileOutputStream != null) fileOutputStream.close();
if (bufferedOutputStream != null) bufferedOutputStream.close();
if (socket != null) socket.close();
}
}
接收文件時,出現以下錯誤:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:128)
at Test.receiveFile(Test.java:211)
at Test.main(Test.java:70)
注意:錯誤在代碼的以下行中:
bufferedOutputStream.write(byteArray, 0 , current);
調試之后,我發現客戶端的輸入流中沒有任何數據,因此read()方法始終返回-1(eof)。 但是服務器正在成功發送文件。
這是服務器的代碼:
public static void sendFile(Socket socket, String fileLocation)
{
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream = null;
File file = new File (fileLocation);
byte [] byteArray = new byte [(int)file.length()];
try {
socket=new Socket(socket.getInetAddress(),port_no);
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(byteArray,0,byteArray.length); // copied file into byteArray
//sending file through socket
outputStream = socket.getOutputStream();
System.out.println("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)");
outputStream.write(byteArray,0,byteArray.length); //copying byteArray to socket
outputStream.flush(); //flushing socket
System.out.println("Done sending!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
這是我對上述方法的調用:
sendFile(clientSocket, "/home/limafoxtrottango/Downloads/serverDownloads/"+sender);
問題是服務器已成功將字節寫入流中,但客戶端的輸入流中似乎沒有任何數據。
https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[],%20int,%20int)
inputStream.read(字節陣列,0,byteArray.length); 在某些情況下,可能會返回-1(如上述文檔所述)。 請迎合這種情況。
另外,我建議對客戶端和服務器使用此處提供的解決方案: 將InputStream寫入Java中文件的有效方法(版本6)
客戶代碼:
final Path destination = Paths.get(fileLocation);
try (
final InputStream in = socket.getInputStream();
) {
Files.copy(in, destination);
}
服務器代碼:
try (
final InputStream in = new FileInputStream(fileLocation);
) {
Files.copy(in, socket.getOutputStream());
}
親切的問候,巴拉
與您的標題相反,服務器未發送任何內容。 它立即關閉了連接,因此bytesRead
最初為-1且從未更改,並且您無法對此進行防御,因此得到ArrayIndexOutOfBoundsException
。
但是,在您發布的代碼中,服務器正在發送內容,但從未關閉套接字,這是您需要修復的另一個錯誤。 它還忽略了FileInputStream.read()
返回的計數,並假定它已填充緩沖區,這不是規范的一部分。
因此,這不是真正的服務器代碼,或者您正在連接其他東西,或者服務器遇到了您未提及的IOException
。
奇怪的是,您使用兩個不同的代碼段進行復制。 用Java復制流的標准方法是:
char buffer = new char[8192]; // or whatever size you prefer > 0
int count;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
在兩端使用它。 不需要英雄般大小的緩沖區,也不需要緩沖區文件的大小,也無需假設文件大小適合int
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.