FtpClient storeFile always return False
Asked Answered
O

5

19

Please figure this out. The code runs properly without any exception.

try
{
    FTPClient ftp = new FTPClient();
    ftp.connect(server);
    if(!ftp.login(username, password))
    {
        ftp.logout();
        return false;
    }
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply))
    {
        ftp.disconnect();
        return false;
    }
    InputStream in = new FileInputStream(localfile);
    ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE);
    ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);
    Store = ftp.storeFile(destinationfile, in);
    in.close();
    ftp.logout();
    ftp.disconnect();
}
catch (Exception ex)
{
    ex.printStackTrace();
    return false;
}
return Store;

Butm the return statement always returns false and the file is not uploaded on the server. Someone please help on this.

For your information, I am in an office network. ---> do we need to add any proxies?


File file = new File("C:\\Users\\sg0214273\\Desktop\\seagate\\seagate.txt");
FileInputStream input = new FileInputStream(file);
client.setFileType(FTP.BINARY_FILE_TYPE);
if (!client.storeFile(file.getName(), input)) {
  System.out.println("upload failed!");
} 
reply = client.getReplyCode();

if(!FTPReply.isPositiveCompletion(reply)) {
  System.out.println("upload failed!");
}
Login success...
230 User ******** logged in.
upload failed!-----> is form boolean return value of storefile 
upload failed!---------> is from replycode...
Logout from FTP server...

Please help out.

Oddfellow answered 16/11, 2011 at 15:26 Comment(0)
C
26

The exact failure message can be found by calling FtpClient#getReplyCode(). From that page (my emphasis):

Immediately after connecting is the only real time you need to check the reply code (because connect is of type void). The convention for all the FTP command methods in FTPClient is such that they either return a boolean value or some other value. The boolean methods return true on a successful completion reply from the FTP server and false on a reply resulting in an error condition or failure. The methods returning a value other than boolean return a value containing the higher level data produced by the FTP command, or null if a reply resulted in an error condition or failure. If you want to access the exact FTP reply code causing a success or failure, you must call getReplyCode after a success or failure.

To see what a return code means, you can see Wikipedia: List of FTP server return codes.

Camiecamila answered 16/11, 2011 at 15:52 Comment(0)
P
8

Topic is quite old but maybe I will help to any other. I compared what FileZilla sends to FTP server and my program did. I needed to use ftp.enterLocalPassiveMode() to make it work, ftp.pasv() no good :)

And for debugging is better to use getReplyString() than only getReplyCode().

Putsch answered 25/1, 2012 at 13:32 Comment(0)
S
6

Modify you code to switch to passive mode before you transfer the file with storeFile() as follows:

...
ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();//Switch to passive mode
Store = ftp.storeFile(destinationfile, in);
in.close();
...

Hope that helps.

Shorn answered 4/2, 2014 at 13:56 Comment(2)
I've just had a problem using Apache commons FTP as the client connecting from a Windows 7 dev machine to a test Windows 7 ftp Server on IIS 7.5. The ftp.storefile returned a ReplyCode of 501 (which claims to be syntax error). The fix was to set passive mode for both server and client using ftp.enterRemotePassiveMode(); ftp.enterLocalPassiveMode();Adachi
when i use this client.enterLocalPassiveMode(); then the reply string is "522 Data connections must be encrypted."Pantry
A
1

please add the apache library for this code this are the impoted class

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

rest import class is from java.io or java.net

public boolean upload(String server,String username,String password,File localfile ){
        boolean Store=false;
        try{
        FTPClient ftp = new FTPClient();
               // ftp.connect(server);
    /* you can use either code which is written above above or below code as ftp port 20 is used for the data transfer and port 21 is used for command and controlls */
           ftp.connect(InetAddress.getByName(server),21); 
    //here 'server' is your domain name of ftp server or url
                if(!ftp.login(username, password))
                {
                    ftp.logout();
                    return false;
                }
          ftp.sendNoOp();//used so server timeout exception will not rise
                int reply = ftp.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply))
                {
                    ftp.disconnect();
                    return false;
                }
              ftp.enterLocalPassiveMode(); /* just include this line here and your code will work fine */
                InputStream in = new FileInputStream(localfile);
              // ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE);
               ftp.setFileType(FTP.BINARY_FILE_TYPE);
               // ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);
                Store = ftp.storeFile(destinationfile, in);
                in.close();
             //ftp.disconnect();
     //here logout will close the connection for you
                ftp.logout();

            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                return false;
            }
        return Store;
    }
Absonant answered 2/3, 2015 at 9:59 Comment(2)
Thanks Naval. ftp.enterLocalPassiveMode(); is really helpful to me.Phyl
when i use this client.enterLocalPassiveMode(); then the reply string is "522 Data connections must be encrypted."Pantry
H
0

Try to use ftp.enterLocalPassiveMode(); before ftp.storeFile(destinationfile, in);

Howardhowarth answered 16/9, 2015 at 0:13 Comment(1)
when i use this client.enterLocalPassiveMode(); then the reply string is "522 Data connections must be encrypted."Pantry

© 2022 - 2024 — McMap. All rights reserved.