【问题标题】:Java File transferring with Sockets error ArrayIndexOutOfBoundsException [closed]带有套接字错误ArrayIndexOutOfBoundsException的Java文件传输[关闭]
【发布时间】:2013-07-25 02:22:50
【问题描述】:

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

【问题讨论】:

  • 我想你可以通过评论你所理解的来节省时间。在任何情况下,此代码都要求您了解 Java 中的流是如何工作的,以及从开发人员的角度(套接字、连接等等)大致了解 TCP 是如何工作的。否则无法解释这段代码的作用。
  • SO 是 NOT 你说我不明白以下任何内容的网站,请解释...
  • 错误与bos.write(mybytearray, 0, bytesRead);有关
  • @Jack 我大致了解 TCP 的工作原理(至少在我看来),我知道如何在两个连接之间发送文本/字符串。

标签: java sockets


【解决方案1】:

发送文件的正确方法:

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

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

【讨论】:

  • 您的发送代码是正确的,您的接收代码不正确。接收代码应与发送代码完全相同,输入和输出相反。您的接收代码没有在正确的时间检测到 EOS,因此会遇到与 OP 相同的问题,并且它也会错误地计算 totalBytesRead。如果您在发送文件后关闭套接字,则根本不需要先发送长度。关闭,但没有雪茄。 – EJP 7 分钟前
  • @EJP 啊,你是对的。修好了。
  • @publ1c_stat1c 感谢您的回答,但我有一个问题。如果我希望服务器发送文件而客户端接收文件,我将把什么作为sendFile(socket, fileName) 的参数“套接字”?我想把我的ServerSocket 对象放在那里,但它说它必须是一个套接字,而不是一个服务器套接字(我可以清楚地看到)
  • @pub1c_stat1c 如果问的不是太多,您能否举例说明如何使用它或再解释一下?我不完全明白我将如何使用它。既然它是布尔类型,我应该像ifwhile 语句一样使用它吗?还是使用类似于void 的方法?
  • @n1ghtk1n9 当您从服务器套接字.accept() 时,返回的对象是一个套接字。用它来交流。我会清理一下代码来帮助你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
相关资源
最近更新 更多