Java:将文件上传到FTP服务器

时间:2016-12-15 11:07:20

标签: java ftp

我正在尝试将文件上传到FTP服务器

正如我在How do you upload a file to an FTP server?找到的那样,我有这段代码:

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

try {
    client.connect("IP");
    client.login("user", "pwd");
    client.changeWorkingDirectory("/a/b/c/");

    // Create an InputStream of the file to be uploaded
    String filePath = file.getPath();
    fis = new FileInputStream(filePath);
    String fileName = file.getName();                               

    // Store file to server
    client.storeFile(fileName, fis);
    client.logout();

} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
    if (fis != null) {
        fis.close();
    }
    client.disconnect();
    } catch (Exception e) {
    e.printStackTrace();
    }
}

当我运行它时,文件会在预期的位置创建,但它是空的(0 kb)

写作过程也需要很长时间......

我做错了什么?

1 个答案:

答案 0 :(得分:-1)

看看这篇文章 Apache Commons FTP problems 您需要设置文件类型和转换模式才能使其正常工作。

相关问题