简体   繁体   English

无法使用套接字发送大文件

[英]Can't send large files with sockets

I am trying to make a Server Client structure thats sends files but I am having problems when I send large files with size more than 200 MB. 我试图使服务器客户端结构可以发送文件,但是当我发送大于200 MB的大文件时遇到了问题。 I send them using ObjectOutputStream with my own object that has the file in a byte array and where I want to save it using the Apache commons libary. 我使用ObjectOutputStream将它们发送给我自己的对象,该对象具有字节数组中的文件,并且希望使用Apache commons库将其保存在其中。

Client SendMethod 客户端SendMethod

try{
        Socket s = new Socket(ip, serverPort);

        ObjectOutputStream output = new ObjectOutputStream(s.getOutputStream());

        FileInputStream file = new FileInputStream(ClientFilePath+"\\"+ClientFileName);

        datainBytes = IOUtils.toByteArray(file);

        Packet paquete = new Packet(datainBytes,ServerFileDirectory,ClientFileName);

        output.writeObject(paquete);
        output.close();

    } catch (ConnectException e){
        //noinspection MagicConstant
        JOptionPane.showMessageDialog(contentPane,"Conexion perdida","ERROR",0);
    } catch(FileNotFoundException e){
        JOptionPane.showMessageDialog(contentPane,"No tienes permisos para movere este archivo","ERROR",0);
    } catch(IOException e) {
        e.printStackTrace();
    }

Server ReceiveMethod 服务器接收方法

Packet paquete = null;

    try {

        //ClientIN is the input from the client initialized earlier
        paquete = (Packet) ClientIN.readObject();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    FileUtils.writeByteArrayToFile(new File(paquete.getFilePathDestiny()+"\\"+paquete.getFileName()), paquete.getData());

The exact problem that I have is the server is trying to read the file but hangs and it does nothing. 我遇到的确切问题是服务器正在尝试读取文件,但挂起,但没有执行任何操作。

Using an Object/Stream is usually only a good idea if you are sending objects. 如果要发送对象,使用对象/流通常只是一个好主意。 In this case you are sending a file which you can stream progressively to the server. 在这种情况下,您将发送一个文件,该文件可以逐步流式传输到服务器。 If you stream the data, it can be of any size eg >> 2 GB. 如果流传输数据,则其大小可以任意,例如>> 2 GB。

Rather than hanging I suspect it's just taking a very long time. 我怀疑这不是花时间,而是挂了很长时间。 Object/Stream are versatile but not very fast for large amounts of data. 对象/流是通用的,但对于大量数据来说不是很快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM