【问题标题】:Writing into text file on ftp server in Java用Java写入ftp服务器上的文本文件
【发布时间】:2014-01-21 04:07:43
【问题描述】:

我的 ftp 服务器上有文本文件。我正在尝试写入此文件,但不能。这是我的代码。

URL  url = new URL("ftp://username:pass@thunder.cise.ufl.edu/public/foler/a.txt;type=i");
URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream(); // To upload
OutputStream buffer = new BufferedOutputStream(os);
ObjectOutput output = new ObjectOutputStream(buffer);
output.writeChars("hello");
buffer.close();
os.close();
output.close();

【问题讨论】:

    标签: java file ftp fwrite


    【解决方案1】:

    ObjectOutputStream 类旨在写入对象数据,以便可以由ObjectInputStream (see here) 重构。它不是用于编写文本文件。如果您只需要编写字符串以更好地进行流式传输,请使用PrintStream

    URL  url = new URL("ftp://username:pass@thunder.cise.ufl.edu/public/foler/a.txt;type=i");
    URLConnection urlc = url.openConnection();
    OutputStream os = urlc.getOutputStream(); // To upload
    OutputStream buffer = new BufferedOutputStream(os);
    PrintStream output = new PrintStream(buffer);
    output.print("hello");
    
    buffer.close();
    os.close();
    output.close();
    

    【讨论】:

    • 如何写入上传的文件而不覆盖其中的数据?
    【解决方案2】:

    你使用什么库?

    我认为连接 FTP 时必须使用正确的 java 库

    我在以前的项目中使用过这个

    ApacheCommons FTPClient

    如果您在使用上述库时遇到问题,请随时询问。

    【讨论】:

      【解决方案3】:

      看看Uploading to FTP using Java的问题,看看用户Loša的回答。

      Loša 的答案唯一缺少的是 var BUFFER_SIZE as 的定义

      final int BUFFER_SIZE = 1024; // or whatever size you think it should be
      

      并为您正在做的事情导入库和基本类定义。

      在此处或通过 DuckDuckGo 或 Google 进行一些简单的搜索就会找到您要查找的内容。

      另外,您问的问题并不是说“这不起作用,我不知道为什么。帮我解决它。”

      【讨论】:

        猜你喜欢
        • 2020-08-06
        • 2012-08-05
        • 2017-08-04
        • 2017-08-26
        • 1970-01-01
        • 1970-01-01
        • 2011-01-06
        • 2013-10-03
        • 1970-01-01
        相关资源
        最近更新 更多