【问题标题】:How can I write to a remote file using Apache Commons Net?如何使用 Apache Commons Net 写入远程文件?
【发布时间】:2016-03-28 16:43:21
【问题描述】:

一旦程序使用 FTPClient connect() 方法连接到服务器,我如何发送字符串并将它们附加到位于远程计算机中的文件?

我已阅读其他帖子,但他们不使用 Apache Commons Net 库。

【问题讨论】:

    标签: java ftp remote-access apache-commons


    【解决方案1】:

    From the docs(您确实检查了文档,对吗?),您需要 FTP 客户端上的 appendFile() 方法。

    类似

    String text = "...."
    String remoteFileName = "..."
    FTPClient ftp = ... // Already connected
    
    try (ByteArrayInputStream local = new ByteArrayInputStream(text.toBytes("UTF-8"))) {
        ftp.appendFile(remoteFilename, local);
    } catch (IOException ex) {
        throw new RuntimeException("Uh-oh", ex);
    }
    

    【讨论】:

    • 非常感谢!又出现了一个问题,为什么要放置“UTF-8”,它有什么区别?再次感谢。
    • "UTF-8" 是远程端在附加您的字符串时将使用的字符集。我选择它作为一个很好的默认设置,但您需要检查您的本地环境。
    猜你喜欢
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2014-03-26
    • 2018-10-04
    • 2013-10-18
    • 2012-07-10
    相关资源
    最近更新 更多