java - readUTF 在套接字文件传输 JAVA 中不从第二次读取

标签 java sockets file-transfer

我正在尝试通过套接字传输文件。我只使用了一个套接字进行通信(我猜不是根据 FTP 协议(protocol))。以下代码将成功传输第一个文件,但无法传输第二个文件,因为文件名不会更改,但服务器会获取新数据文件的读取字节。我认为问题出在 readUTF 和 writeUTF 上。

这是我的服务器端代码。 请记住,这是接受文件。而不是发送文件。

public int listenPort() throws IOException{
    System.out.println("LISTENING");
            try{
                //this.dis = new DataInputStream(this.socketClient.getInputStream());
                if( this.dis.available() != 0 ){

                String filename = this.dis.readUTF();
                this.fos = new FileOutputStream("/home/ankit07/" + filename);

                int bytesRead = (int) IOUtils.copyLarge(this.dis,this.fos); //no of bytes copied
                return bytesRead;

                }else{
                    return 0;
                }
            }finally{

            }
}

这是我的客户端。 记住此方发送文件。不接受

public void getFile(String filename) throws IOException{
            try{
                this.file = this.window.file;
                DataOutputStream dos = new DataOutputStream(this.socketClient.getOutputStream());
                dos.writeUTF(filename);
                FileInputStream fis = new FileInputStream(this.file);
                int readByte = (int) IOUtils.copyLarge(fis, dos);
                System.out.println("FILE SENT : " + filename + "  Bytes :" + readByte);

                //this.socketClient.close();
            }finally{               
                //if( this.os!=null)    this.os.close();
                if( this.window.file != null) this.window.file = null;
                if( this.file != null) this.file = null;
                //if( this.socketClient!=null) this.socketClient.close();
            }
}

文件选择是在其他类窗口中完成的。

选择文件的方法在窗口类中。它有一个公共(public) File 属性来保存文件,然后我调用 getFile(String filename) 来发送文件名并引用所选文件,客户端有 File 属性来引用同一文件。

public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    Object src = e.getSource();

    if( src instanceof JButton ){   //Browse clicked
        JFileChooser fc = new JFileChooser(); 
        int returnVal = fc.showDialog(null, "SELECT FILE");
        if( returnVal == JFileChooser.APPROVE_OPTION){
                this.file = fc.getSelectedFile();
                try {
                    this.sc.getFile(this.file.getName());
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }else{
            //unable to select file 
        }
    }
}

此外,除了我最初的问题之外,我无法传输 mp3 和视频等大文件。如果您知道任何解决方案,将会很有帮助。 谢谢你!!!!

最佳答案

您正在发送文件名,但随后您正在发送this.file。这里没有任何东西表明它们总是引用同一个文件。事实上,您有一个 RS 文件,位于 this.window.file 中。您需要解决所有这些困惑。

关于java - readUTF 在套接字文件传输 JAVA 中不从第二次读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31799917/

相关文章:

java - 如何定义和初始化一个生成 2 到 20(含)随机整数的变量?

java - Camel 泉中的多条内部路线

python - 如何在 Python 中创建异步套接字?

c# - 如何在C#中使用IP地址将文件传输到共享文件夹

c# - 我应该在 SharpZipLib 中选择哪种压缩类型?

java - 是否忽略了 HTTP 服务器 header 响应?

java - 没有为 Cucumber runner 注入(inject) Spring 依赖项

java - jReddit Java 异常

java - InputStream的read()方法是如何实现的?

node.js - 如何在 2 个 node.js 实例之间高效地传输文件?