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();
}
}
そしてここでは、上記の方法に私の呼び出しですクライアントは入力ストリームにデータを持っていないようです。
[何がjava.lang.ArrayIndexOutOfBoundsExceptionを引き起こし、どのように防止するのですか?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-どうすればいいですか) –
IDEデバッガのコードをステップ実行してみてください。あなたが何が間違っているか教えてくれるよりも、ずっと多くのことを学ぶでしょう。 –
関連しないコメント:これはタブを使用しない理由です。これは、すべてのエディタで異なってレンダリングされます。-_-は、コードをインデントする人のために人生の地獄を作ります.SO – rafid059