2017-02-04 19 views
0

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)で、クライアントが任意のデータを持っていないこと。しかし、サーバーはファイルを正常に送信しています。ここで

は、サーバーのコードです:事は、サーバーが正常にストリームにバイトを書き込んでいるということです

sendFile(clientSocket, "/home/limafoxtrottango/Downloads/serverDownloads/"+sender); 

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(); 
    } 
} 

そしてここでは、上記の方法に私の呼び出しですクライアントは入力ストリームにデータを持っていないようです。

+1

[何がjava.lang.ArrayIndexOutOfBoundsExceptionを引き起こし、どのように防止するのですか?](http://stackoverflow.com/questions/5554734/what-c​​auses-a-java-lang-arrayindexoutofboundsexception-and-どうすればいいですか) –

+3

IDEデバッガのコードをステップ実行してみてください。あなたが何が間違っているか教えてくれるよりも、ずっと多くのことを学ぶでしょう。 –

+0

関連しないコメント:これはタブを使用しない理由です。これは、すべてのエディタで異なってレンダリングされます。-_-は、コードをインデントする人のために人生の地獄を作ります.SO – rafid059

答えて

0

https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[],%20int,%20int)

inputStream.read(BYTEARRAY、0、byteArray.length)。上のドキュメントで与えられているように-1を返すことがあります。そのような状況に対応してください。また

、私はクライアントとサーバの両方のために、ここで指定したソリューションを使用することをお勧め:Efficient way to write InputStream to a File in Java (Version 6)

クライアントコード:

final Path destination = Paths.get(fileLocation); 
try (
    final InputStream in = socket.getInputStream(); 
) { 
    Files.copy(in, destination); 
} 

Serverコード:

try (
    final InputStream in = new FileInputStream(fileLocation); 
) { 
    Files.copy(in, socket.getOutputStream()); 
} 

敬具、 Bala

+0

私はドキュメントを読んで、eofに達したときにメソッドが-1を返すと言います。それは常に-1を返すので、私はストリーム内のデータを受け取っていないと推測します。第二の解決策として、私はそれを試してみる。 –

+0

また、2番目の方法では、ダウンロードしたファイルのサイズは0Bです。だから、ファイルにコピーするデータがないと思う。 –

+0

@PrashantPandeyあなたのサーバーがソケットに0バイトを与え続けているなら、私は問題がこのコードの外にあると信じています。しかし、そのような状況のために、このコードにすべての小切手を入れておくとよいでしょう。 :) – BhalchandraSW

0

サーバーisnあなたのタイトルに反して、何も送っていない。それはすぐに接続を閉じているので、bytesReadは最初は-1であり、決して変更されず、それに対して防御していないので、ArrayIndexOutOfBoundsExceptionが得られます。

あなたが投稿したコードでは、サーバーは何かを送信していますが、決してソケットを閉じることはありません。これは修正する必要がある別のバグです。また、FileInputStream.read()によって返されたカウントを無視しており、それが仕様の一部ではないバッファを満たしていると仮定しています。

これは実際のサーバーコードではないか、または何かに接続しているか、またはサーバーに記載されていないIOExceptionがあります。

2つの異なるコードをコピーするのは興味深いです。 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に収まっていると仮定します。

+0

@downvoterご説明ください。それとも、サイトの破壊行為ですか? – EJP

+0

変更をすぐに実装する。私は長い間サーバーコードを見ていないと思う。いくつかの非常に素朴なエラーが含まれています。これは恥ずかしいです。 –

関連する問題