【问题标题】:FtpClient storeFile always return FalseFtpClient storeFile 总是返回 False
【发布时间】:2011-12-30 13:06:34
【问题描述】:

请解决这个问题。代码运行正常,没有任何异常。

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 并且文件没有上传到服务器上。有人请帮忙。

供您参考,我在办公网络中。 ---> 我们需要添加任何代理吗?


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

请帮忙。

【问题讨论】:

    标签: java ftp ftp-client apache-commons-net


    【解决方案1】:

    可以通过调用FtpClient#getReplyCode() 找到确切的失败消息。从该页面(我的重点):

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

    要了解返回码的含义,您可以查看Wikipedia: List of FTP server return codes

    【讨论】:

      【解决方案2】:

      话题已经很老了,但也许我会对其他人有所帮助。我比较了 FileZilla 发送到 FTP 服务器的内容和我的程序所做的。我需要使用ftp.enterLocalPassiveMode() 使其工作,ftp.pasv() 不好:)

      对于调试来说,使用getReplyString() 比只使用getReplyCode() 更好。

      【讨论】:

        【解决方案3】:

        修改代码以在使用storeFile() 传输文件之前切换到被动模式,如下所示:

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

        希望对您有所帮助。

        【讨论】:

        • 我刚刚在使用 Apache commons FTP 作为从 Windows 7 开发机器连接到 IIS 7.5 上的测试 Windows 7 ftp 服务器的客户端时遇到了问题。 ftp.storefile 返回一个ReplyCode 501(声称是语法错误)。解决方法是使用 ftp.enterRemotePassiveMode(); 为服务器和客户端设置被动模式; ftp.enterLocalPassiveMode();
        • 当我使用这个 client.enterLocalPassiveMode();那么回复字符串是“522 数据连接必须加密。”
        【解决方案4】:

        请为此代码添加 apache 库 这是被引入的类

        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 导入类来自 java.io 或 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;
            }
        

        【讨论】:

        • 谢谢海军。 ftp.enterLocalPassiveMode();对我真的很有帮助。
        • 当我使用这个client.enterLocalPassiveMode(); 时,回复字符串是“522 数据连接必须加密。”
        【解决方案5】:

        尝试使用 ftp.enterLocalPassiveMode();之前 ftp.storeFile(destinationfile, in);

        【讨论】:

        • 当我使用这个 client.enterLocalPassiveMode();那么回复字符串是“522 数据连接必须加密。”
        猜你喜欢
        • 2017-04-26
        • 2012-01-05
        • 1970-01-01
        • 1970-01-01
        • 2016-07-20
        • 2021-11-05
        • 2018-11-05
        • 2019-05-06
        相关资源
        最近更新 更多