FtpClient storeFile 总是返回 False

时间:2023-01-16
本文介绍了FtpClient storeFile 总是返回 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;

但是 return 语句总是返回 false 并且文件没有上传到服务器上.有人请帮忙.

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...

请帮忙.

推荐答案

可以通过调用FtpClient#getReplyCode().从该页面(我的重点):

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

连接后立即是您需要检查的唯一实时时间回复代码(因为 connect 是 void 类型).该公约为FTPClient 中的所有 FTP 命令方法都是这样的返回一个布尔值或其他一些值.布尔方法返回如果来自 FTP 服务器的成功完成回复,则为 true,如果为 false导致错误情况或失败的回复.方法返回一个布尔值以外的值,返回一个包含由 FTP 命令生成的更高级别的数据,如果回复则为 null导致错误情况或失败.如果您想访问导致成功或失败的确切 FTP 回复代码,您必须调用在成功或失败后获取回复代码.

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.

要了解返回码的含义,您可以查看维基百科:FTP 服务器返回码列表.

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

这篇关于FtpClient storeFile 总是返回 False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:我应该使用什么 Java FTP 客户端库? 下一篇:检查 FTP 服务器上的文件是否存在

相关文章

最新文章