0

I want to upload a text file on a FTP using apache commons=net 1.4.1.

My code:

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();
        }
    }

This doesn't catch any errors but the file is not uploaded.

Where I'm wrong?

I found that

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

If i put that code i get the text false in output log, and true at ftp.login. What can be the prob

Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94

1 Answers1

1

My code didn't worked because i was trying to store a file on the ftp adress + the ftp adress + file name, i only need to put the name of the file not the ftp again.

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.

Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94