0

I want to upload a file in a specific path in an ftp server the code is quite simple:

public static void main(String[] args) {
String server = "xx.xx.xx.xx";
String user = "xxx";
String pass = "xxx";

FTPClient ftpClient = new FTPClient();
try {

    ftpClient.connect(server);
    System.out.println("Connected to " + server + ".");
    System.out.print(ftpClient.getReplyString());

    ftpClient.login(user, pass);
    ftpClient.enterLocalPassiveMode();

    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

    // uploads first file using an InputStream
    File firstLocalFile = new File("/tmp/PAR.TXT");

    String firstRemoteFile = "/DATA/OUTFILES/PAR.TXT";
    InputStream inputStream = new FileInputStream(firstLocalFile);

    System.out.println("Start uploading first file");
    boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
    System.out.println("done:"+done);

    inputStream.close();
    if (done) {
        System.out.println("The file is uploaded successfully.");
    }


} catch (IOException ex) {
    System.out.println("Error: " + ex.getMessage());
    ex.printStackTrace();
} finally {
    try {
        if (ftpClient.isConnected()) {
            ftpClient.logout();
            ftpClient.disconnect();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

}

I always get done = false.

Here's the result:

Connected to xx.xx.xx.xx.
220 "Welcome (logging activated)"
Start uploading file
done:false

I printed the FtpClient#getReplyCode(). and i get this:

500 Illegal PORT command.
Amira
  • 3,184
  • 13
  • 60
  • 95

2 Answers2

2

You can only access files relative to the root folder of the ftp server. You need to configure your ftp server to add a virtual folder pointing to the path you want.

Tarik
  • 10,810
  • 2
  • 26
  • 40
1

I passed to PassiveMode and it works now

Amira
  • 3,184
  • 13
  • 60
  • 95
  • 1
    Just to clarify for other users, to enter the passive mode do this: ftpClient.enterLocalPassiveMode(); – Dmitry May 18 '18 at 11:35