19

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

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

请帮忙。

4

5 回答 5

26

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

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

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

于 2011-11-16T15:52:48.600 回答
7

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

并且对于调试来说getReplyString()比只使用getReplyCode().

于 2012-01-25T13:32:24.147 回答
5

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

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

希望有帮助。

于 2014-02-04T13:56:15.160 回答
1

请为此代码添加 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;
    }
于 2015-03-02T09:59:36.897 回答
0

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

于 2015-09-16T00:13:38.407 回答