-4

当我运行客户端时,它返回(一个错误): Sockets 的 java.io.BufferedOutputStream.write(Unknown Source) 的 java.lang.System.arraycopy(Native Method) 的线程“main”java.lang.ArrayIndexOutOfBoundsException 中的异常.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 回答