【问题标题】:java common ftpclient upload text filejava常用ftpclient上传文本文件
【发布时间】:2014-03-14 15:42:41
【问题描述】:

我想使用 apache commons=net 1.4.1 在 FTP 上上传一个文本文件。

我的代码:

public static void UploadTextFile(String host, String name) {
        try {
            FTPClient ftp = new FTPClient();

            ftp.connect(host);
            int reply;
            ftp.login("u757471605", "cacat1");
            String str = "GG FRT O IESIT";
            InputStream input = new ByteArrayInputStream(str.getBytes());
            String loc = host + "/public_html/" + name;
            ftp.storeFile(loc, input);
            ftp.disconnect();
            System.out.println(loc);
            // this.ftp.storeFile(hostDir + fileName, input);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

这不会捕获任何错误,但文件没有上传。

我哪里错了?

我发现了

System.out.println(ftp.storeFile(loc, input))

如果我输入该代码,我会在输出日志中得到错误的文本,而在 ftp.login 中得到正确的文本。 可能是什么问题

【问题讨论】:

    标签: java ftp apache-commons-net


    【解决方案1】:

    我的代码不起作用,因为我试图将文件存储在 ftp 地址 + ftp 地址 + 文件名上,我只需要再次输入文件名而不是 ftp。

    public static void UploadTextFile(String host, String name) {
        try {
            FTPClient ftp = new FTPClient();
    
            ftp.connect(host);
            int reply;
            System.out.println(ftp.login("u757471605", "cacat1"));
    
            ftp.enterLocalPassiveMode();
            System.out.println(ftp.getReplyString());
            String str = "GG FRT O IESIT";
            InputStream input = new ByteArrayInputStream(str.getBytes());
            String loc = "/public_html/" + name;
    
            System.out.println(ftp.storeFile(loc, input));
            System.out.println(ftp.getReplyString());
            ftp.disconnect();
            System.out.println(loc);
            // this.ftp.storeFile(hostDir + fileName, input);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    是的。

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 2013-04-07
      • 2015-10-06
      • 2013-07-07
      • 2012-05-18
      • 1970-01-01
      • 2014-06-18
      • 2021-06-25
      • 1970-01-01
      相关资源
      最近更新 更多