2011-11-16 290 views
13

請解釋清楚。代碼正常運行,沒有任何異常。FtpClient storeFile總是返回False

 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; 

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

請大家幫幫忙

+0

ftp.getReplyCode()返回什麼?在ftp.storeFile之後? –

+0

@Gray no。看第一句 –

回答

17

準確的故障信息可以通過調用FtpClient#getReplyCode()找到。從該頁面(我的重點):

立即連接後,您是唯一的實時您需要檢查 答覆代碼(因爲連接是void類型)。對於 FTPClient中的所有FTP命令方法的約定是這樣的:它們可以是 返回一個布爾值或其他值。返回 的布爾方法返回來自FTP服務器的成功完成答覆,返回false返回 導致錯誤狀態或失敗的答覆。返回非布爾值的方法 將返回包含由FTP命令生成的更高級別數據的值的值,如果應答 導致錯誤條件或失敗,則返回null。 如果您想訪問 確切的FTP回覆代碼導致成功或失敗,您必須在成功或失敗後調用 getReplyCode。

要查看返回碼的含義,可以看到Wikipedia: List of FTP server return codes

+0

謝謝你的回覆。問題依然存在。 File file = new File(「C:\\ Users \\ sg0214273 \\ Desktop \\ seagate \\ seagate.txt」); \t FileInputStream input = new FileInputStream(file); \t client.setFileType(FTP.BINARY_FILE_TYPE); (!client.storeFile(file.getName(),input)){ \t System.out.println(「upload failed!」); \t} \t reply = client.getReplyCode(); \t \t如果(FTPReply.isPositiveCompletion(答覆)!){ \t \t \t的System.out.println( 「上傳失敗!」); \t \t} 輸出是 上傳失敗! 上傳失敗! – RaviKiran

+1

是的,但getReplyCode的*值*是多少。這是FTP回覆代碼:請參閱http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes –

+0

getReplyCode的值在登錄後爲230,在storefile爲550後(後面是immedialtly) – RaviKiran

5

主題是相當古老,但也許我會幫助任何其他。我比較了FileZilla發送到FTP服務器和我的程序。我需要使用ftp.enterLocalPassiveMode(),使其工作,ftp.pasv()沒有什麼好:)

進行調試是更好地利用getReplyString()不是僅僅getReplyCode()

2

修改你的代碼切換到被動模式,然後按如下方式傳輸文件storeFile()

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

希望有所幫助。

+0

我剛剛有一個問題,使用Apache公共FTP作爲客戶端連接從Windows 7開發機器到IIS 7.5上的測試Windows 7 ftp服務器。 ftp.storefile返回501的一個ReplyCode(聲稱是語法錯誤)。解決方法是使用 ftp.enterRemotePassiveMode()爲服務器和客戶端設置被動模式; ftp.enterLocalPassiveMode(); – MartinS

1

請加阿帕奇庫這段代碼 這是impoted類

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; 

其餘進口類是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; 
    } 
+0

謝謝海軍。 ftp.enterLocalPassiveMode();對我真的很有幫助。 – yaryan997

0

嘗試使用ftp.enterLocalPassiveMode ();在ftp.storeFile(destinationfile,in)之前;