带套接字的Java文件传输错误ArrayIndexOutOfBoundsException

带套接字的Java文件传输错误ArrayIndexOutOfBoundsException,java,sockets,Java,Sockets,运行客户端时,它返回(错误):线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常 位于java.lang.System.arraycopy(本机方法) 位于java.io.BufferedOutputStream.write(未知源) 位于Sockets.FileSocketClient.main(FileSocketClient.java:14) 我知道它发生在哪里[bos.write(mybytearray,0,bytesRead);],

运行客户端时,它返回(错误):线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常 位于java.lang.System.arraycopy(本机方法) 位于java.io.BufferedOutputStream.write(未知源) 位于Sockets.FileSocketClient.main(FileSocketClient.java:14)

我知道它发生在哪里[bos.write(mybytearray,0,bytesRead);],我只是不明白为什么

服务器 }

客户
发送文件的正确方式:

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


此代码与您的代码之间的区别在于,在发送整个文件之前,我首先发送整个文件的长度。该文件可能比您为其分配的缓冲区大,因此您需要一个循环以增量方式读取它。

我想您可以通过注释您理解的内容来节省时间。在任何情况下,这段代码都要求您了解流在Java中是如何工作的,以及从开发人员的角度(套接字、连接等)大致了解TCP是如何工作的。否则,无法解释此代码的作用。因此,您不是说我不理解以下任何内容的网站,请解释…错误与
bos.write(mybytearray,0,ByteRead)有关@Jack我确实大致了解TCP的工作原理(至少在我看来),我知道如何在两个连接之间发送文本/字符串。您的发送代码是正确的,而您的接收代码不是。接收代码应与发送代码完全相同,输入和输出相反。您的接收代码没有在正确的时间检测到EOS,因此将遇到与OP相同的问题,并且它还错误计算totalBytesRead。如果在发送文件后关闭套接字,则根本不需要先发送长度。接近,但没有雪茄EJP 7分钟ago@EJP啊,你说得对。修正了。@publ1c\u stat1c谢谢你的回答,但我有一个问题。如果我想让服务器发送文件,让客户端接收文件,我会把什么作为
sendFile(socket,fileName)
的参数“socket”?我想把我的
ServerSocket
对象放在那里,但它说它必须是一个socket,而不是一个ServerSocket(我可以清楚地看到)@pub1c\u stat1c如果问得不多,你能给出一个如何使用它的例子或解释更多吗?我不完全明白我将如何使用它。既然它是布尔类型,我应该像
if
while
语句一样使用它吗?或者使用类似于
void
方法?@n1ghtk1n9当您从serversocket中
.accept()
时,返回的对象是套接字。用它来交流。我会整理一下代码以帮助您。
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();
}