-4

クライアントを実行すると、(エラー) が返されます: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at java.io.BufferedOutputStream.write(Unknown Source) at Sockets .FileSocketClient.main(FileSocketClient.java:14)

どこで発生しているのか理解しています[bos.write(mybytearray, 0, bytesRead);]、理由がわかりません

サーバ

import java.io.*;  
import java.net.*;

public class FileSocketServer {

public static void main(String args[]) throws IOException {
    ServerSocket serverSocket = new ServerSocket(1235);
    File myFile = new File("test.txt");

    while(true) {
        Socket socket = serverSocket.accept(); //Understand 
        byte[] mybytearray = new byte[(int)myFile.length()]; //Don't understand
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); //Don't understand
        bis.read(mybytearray, 0, mybytearray.length); //Don't understand
        OutputStream os = socket.getOutputStream(); //Don't understand
        os.write(mybytearray, 0, mybytearray.length); //Don't understand
        os.flush(); //Don't understand
        socket.close(); //Don't understand
    }
}

}

クライアント

package Sockets;

import java.io.*;
import java.net.*;

public class FileSocketClient {
public static void main(String args[]) throws IOException{
    Socket socket = new Socket("GANNON-PC", 1235); //Understand
    byte[] mybytearray = new byte[1024]; //Don't understand
    InputStream is = socket.getInputStream(); //Don't understand
    FileOutputStream fos = new FileOutputStream("mods//test.txt"); //Don't understand
    BufferedOutputStream bos = new BufferedOutputStream(fos); //Don't understand
    int bytesRead = is.read(mybytearray, 0, mybytearray.length); //Don't understand
    bos.write(mybytearray, 0, bytesRead); //Don't understand
    bos.close(); //
    socket.close();
}
}
4

1 に答える 1

0

ファイルを送信する適切な方法:

public void sendFile(Socket socket, File myFile) throws IOException  {
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); //get the output stream of the socket
    dos.writeInt((int) myFile.length()); //write in the length of the file

    InputStream in = new FileInputStream(myFile); //create an inputstream from the file
    OutputStream out = socket.getOutputStream(); //get output stream
    byte[] buf = new byte[8192]; //create buffer
    int len = 0;
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len); //write buffer
    }
    in.close(); //clean up
    out.close();
}

ファイルを受け取る:

public void receiveFile(Socket socket, String fileName) throws IOException {
        DataInputStream dis = new DataInputStream(socket.getInputStream()); //get the socket's input stream
        int size = dis.readInt(); //get the size of the file.
        InputStream in = socket.getInputStream(); 
        OutputStream out = new FileOutputStream(fileName); //stream to write out file
        int totalBytesRead = 0;
        byte[] buf = new byte[8192]; //buffer
        int len = 0;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len); //write buffer
        }

        out.close(); //clean up
        in.close();
}

このコードとあなたのコードの違いは、ファイル全体を送信する前に、まずファイルの長さを送信することです。ファイルは、割り当てたバッファよりも大きい可能性があるため、ループを使用してインクリメンタルに読み取る必要があります。

于 2013-07-25T02:40:37.063 に答える