【问题标题】:apache commons ftp client file upload issueapache commons ftp客户端文件上传问题
【发布时间】:2012-03-20 09:02:41
【问题描述】:

我的机器上本地的 excel 文件很少。我想将此excel文件上传到ftp。我正在使用 apache commons FTPClinet。但是当它上传文件时,它已损坏并且大小为零。这是示例程序

谁能指出我哪里做错了?

public class App {
    private static final String server = "localhost";
    private static final String username = "test";
    private static final String password = "test";
    private static final String directory = "/home/files";

    public static void main(String[] args) throws SocketException, IOException {
        FTPClient f = new FTPClient();
        f.connect(server);
        f.login(username, password);
        f.setFileType(FTPClient.BINARY_FILE_TYPE);
        InputStream is = null;

        is = new FileInputStream("c:\\tmp\\output.xls");
        Boolean isStored = f.storeFile("status.xls", is);
        is.close();
    }
}

【问题讨论】:

  • 我没有收到任何错误消息,唯一的问题是文件已损坏并且没有写入数据
  • 你能发布你的错误吗?
  • 您确定/home/files/status.txt 在FTP 服务器目录结构的上下文中有效吗?这看起来像是在服务器的文件存储根目录下可能无效的绝对路径。
  • 查看更新后的代码,它现在正在生成一个 excel 文件,但仍然有一些不可读的字符损坏

标签: java apache-commons-net


【解决方案1】:

不要使用f.setFileType(FTPClient.BINARY_FILE_TYPE); 删除该行。

Excel 文件不是二进制文件,它们应该作为 ascii 传输,我认为这是 apache commons FTPClient 的默认设置。

我没有测试过这个。试试看吧。

也是从InputStream更改为FileInputStream

【讨论】:

  • 没有成功我的朋友。这次我没有看到不可读的内容,但我仍然在复制的文件中看到完整的数据。
  • 只是确保在您的代码中,服务器名称不是 localhost,对吧?
  • 如果您尝试将其上传到的是 linux 服务器,请确保该文件夹具有足够的权限 (777)
  • 正确,我使用的是实际主机名。并回答 roymustang86,我给了这个权限,所以它允许创建文件但它已损坏
  • 是不是因为 excel 文件是在 windows os 中创建的,然后通过 ftp 客户端复制到 linux 上,所以损坏了?
猜你喜欢
  • 1970-01-01
  • 2014-08-09
  • 2011-10-02
  • 2018-07-07
  • 2012-12-11
  • 1970-01-01
  • 2015-07-20
  • 2012-01-25
  • 2011-11-13
相关资源
最近更新 更多