Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
987 views
in Technique[技术] by (71.8m points)

java - FtpClient storeFile always return False

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...