java常见的ftpclient上传文本文件

时间:2014-03-14 15:42:42

标签: java ftp apache-commons-net

我想使用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))

如果我输入该代码,我会在输出日志中显示false,而在ftp.login中则为true。 什么可能是问题

1 个答案:

答案 0 :(得分: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();
    }
}

Yaay。

相关问题