Apache commons FPTSClient 显式传输文件不完整

发布于 2025-01-14 19:18:21 字数 2297 浏览 2 评论 0原文

我目前在使用 Apache Commons 的 FTPSClient 方面遇到了一些困难。请参阅下面的代码。当使用 FTPClient 时,我尝试将文件写入 FTP 服务器(vsftpd),一切正常。当使用我的代码片段时,在 Util.copyStream() 返回一切正常或设置 Thread.sleep(100) 后进行调试和等待时,我总是会收到 451 错误。当我不设置 ftpsClient.execProt("P) 时,也不会发生这种情况。有谁知道这可能是由什么引起的。

final FTPSClient client;
        client = new FTPSClient("TLS", false);

        client.setUseClientMode(true);
        client.setDefaultPort(21);

        // connect
        try {
            client.connect("serverAddress", 21);
        } catch (SSLException e) {
            throw e;
        }

        // setup any after connected
        client.setSoTimeout(300);
        client.setListHiddenFiles(true);
        client.enterLocalPassiveMode();

        FTPClientConfig ftpConfig;
        try {
            ftpConfig = new FTPClientConfig(client.getSystemType());
        } catch (FTPConnectionClosedException e) {
            throw e;
        } catch (IOException e) {
            ftpConfig = new FTPClientConfig();
        }
        client.configure(ftpConfig);

        final FTPSClient ftpsClient = client;
        // remove data buffer limit
        ftpsClient.execPBSZ(0);
        // set data channel encrypted
        ftpsClient.execPROT("P");

        client.login("user", "password");
        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            throw new IOException("Authentication failed: " + client.getReplyString().trim());
        }

        // postconfigure connection
        if (!client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE) || !client.setFileType(FTP.BINARY_FILE_TYPE)) {
            throw new IOException("Failed to correctly configure client: " + client.getReplyString().trim());
        }

        InputStream input;
        OutputStream output;
        input = new FileInputStream(pathToLocalFile);
        output = client.storeFileStream("foobar.txt");
        final var number = Util.copyStream(input, output);
        System.out.println(number);
        input.close();
        // Thread.sleep(100);
        output.close();
        // Must call completePendingCommand() to finish command.
        if (!client.completePendingCommand()) {
             client.logout();
             client.disconnect();
            System.err.println("File transfer failed.");
        }

Iam currently struggling a little bit with the FTPSClient from Apache Commons. See code down below. I try to write a file to FTP Server (vsftpd) when using FTPClient things are working perfectly fine. When using my code snippet I will always get a 451 Error, when debugging and waiting after Util.copyStream() returned everything works fine or settings a Thread.sleep(100). This also does not happen when I do not set the ftpsClient.execProt("P). Does anyone know by what this could be caused.

final FTPSClient client;
        client = new FTPSClient("TLS", false);

        client.setUseClientMode(true);
        client.setDefaultPort(21);

        // connect
        try {
            client.connect("serverAddress", 21);
        } catch (SSLException e) {
            throw e;
        }

        // setup any after connected
        client.setSoTimeout(300);
        client.setListHiddenFiles(true);
        client.enterLocalPassiveMode();

        FTPClientConfig ftpConfig;
        try {
            ftpConfig = new FTPClientConfig(client.getSystemType());
        } catch (FTPConnectionClosedException e) {
            throw e;
        } catch (IOException e) {
            ftpConfig = new FTPClientConfig();
        }
        client.configure(ftpConfig);

        final FTPSClient ftpsClient = client;
        // remove data buffer limit
        ftpsClient.execPBSZ(0);
        // set data channel encrypted
        ftpsClient.execPROT("P");

        client.login("user", "password");
        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            throw new IOException("Authentication failed: " + client.getReplyString().trim());
        }

        // postconfigure connection
        if (!client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE) || !client.setFileType(FTP.BINARY_FILE_TYPE)) {
            throw new IOException("Failed to correctly configure client: " + client.getReplyString().trim());
        }

        InputStream input;
        OutputStream output;
        input = new FileInputStream(pathToLocalFile);
        output = client.storeFileStream("foobar.txt");
        final var number = Util.copyStream(input, output);
        System.out.println(number);
        input.close();
        // Thread.sleep(100);
        output.close();
        // Must call completePendingCommand() to finish command.
        if (!client.completePendingCommand()) {
             client.logout();
             client.disconnect();
            System.err.println("File transfer failed.");
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2025-01-21 19:18:21

这个库已经存在很长时间了,并且内部情况发生了一些变化。尝试:

    input = new FileInputStream(pathToLocalFile);
    boolean result = client.storeFile("foobar.txt", input);
    if (result) {
           System.out.println("\tFile Transfer Completed Successfully");
    }

我注意到,每隔一段时间将文件传输到大型机时,它就无法完成。我认为这与文件长度有关,但我一直无法找到它。我也不使用stream_transfer_mode。

This library has been around for a long time, and things change a bit under the hood. Try:

    input = new FileInputStream(pathToLocalFile);
    boolean result = client.storeFile("foobar.txt", input);
    if (result) {
           System.out.println("\tFile Transfer Completed Successfully");
    }

I have noticed that every once in a while when transferring files to a mainframe, it won't complete. I think it has something to do with the file length, but I've never been able to track it down. I also don't use the stream_transfer_mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文