FtpClient storeFile 总是返回 False

FtpClient storeFile always return False(FtpClient storeFile 总是返回 False)
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Show progress during FTP file upload in a java applet(在 Java 小程序中显示 FTP 文件上传期间的进度)
How to copy a file on the FTP server to a directory on the same server in Java?(java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?)
FTP zip upload is corrupted sometimes(FTP zip 上传有时会损坏)
Enable logging in Apache Commons Net for FTP protocol(在 Apache Commons Net 中为 FTP 协议启用日志记录)
Checking file existence on FTP server(检查 FTP 服务器上的文件是否存在)
What Java FTP client library should I use?(我应该使用什么 Java FTP 客户端库?)