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

时间:2013-07-25 02:22:50

标签: java sockets

当我运行客户端时,它返回(错误):线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException     在java.lang.System.arraycopy(本机方法)     at java.io.BufferedOutputStream.write(Unknown Source)     在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();
}
}

1 个答案:

答案 0 :(得分: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();
}

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