FtpClient storeFile总是返回False

Rav*_*ran 16 java ftp-client apache-commons-net

请弄清楚这一点.代码正常运行,没有任何异常.

        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;
Run Code Online (Sandbox Code Playgroud)

Buttttttttt

return语句始终返回false,并且文件未上载到服务器上.有人请帮忙.

为了您的信息,1)我在办公室网络.--->我们需要添加任何代理吗?


          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...
Run Code Online (Sandbox Code Playgroud)

请帮忙

Mat*_*ell 24

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

连接后立即是您需要检查答复代码的唯一实时时间(因为连接的类型为void).FTPClient中所有FTP命令方法的约定是它们返回布尔值或其他值.布尔方法在来自FTP服务器的成功完成答复时返回true,在答复时返回false,从而导致错误条件或失败.返回布尔值以外的值的方法返回包含FTP命令生成的更高级别数据的值,如果答复导致错误条件或失败,则返回null.如果要访问导致成功或失败的确切FTP回复代码,则必须在成功或失败后调用getReplyCode.

要查看返回代码的含义,您可以看到Wikipedia:FTP服务器返回代码列表.

  • 是的,但是getReplyCode的“值”是什么。这是FTP回复代码:请参阅http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes (2认同)

jas*_*siu 5

话题很老但也许我会帮助其他任何人.我比较了FileZilla发送到FTP服务器和我的程序所做的事情.我需要使用ftp.enterLocalPassiveMode()使其工作,ftp.pasv()没有好:)

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


Bab*_*emi 5

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

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

希望有帮助。