FTPClient - Java,上传文件

2022-01-09 00:00:00 ftp ftp-client java apache-commons-net

我正在尝试进行非常简单的文件上传.我想要一个可以上传我告诉它的任何文件的 Java FTPClient.但是 pdf 总是一团糟,我的 pdf 编辑器 (Adobe) 不会打开它,说存在 I/O 错误.

I'm trying to do a VERY simple file upload. I want a Java FTPClient that can upload any file I tell it to. But the pdf always gets all messed up and my pdf editor (Adobe) won't open it, saying there is an I/O error.

我正在使用以下类:

    import org.apache.commons.net.ftp.FTPClient;
    ....

    FTPClient client = new FTPClient();
    FileInputStream fis = null;

    try {
        client.connect("mydomain.com");
        client.login("user", "password");

        String filename = "myPDF.pdf";
        fis = new FileInputStream(filename);

        client.storeFile("temp.pdf", fis);
        fis.close();
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    }

为什么这不起作用,我该如何解决?

Why doesn't this work, and how do I fix it?

推荐答案

因为 FTPClient 是 FTP.ASCII_FILE_TYPE.您只需要更新配置即可以二进制模式传输.

It doesn't work because the default transfer mode for FTPClient is FTP.ASCII_FILE_TYPE. You just need to update the configuration to transfer in binary mode.

相关文章