FTPClient - Java,上传文件 [英] FTPClient - Java, upload file

查看:38
本文介绍了FTPClient - Java,上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行非常简单的文件上传.我想要一个可以上传我告诉它的任何文件的 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?

推荐答案

因为 FTPClientFTP.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.

这篇关于FTPClient - Java,上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆