2014-02-20 81 views
0

我能夠連接Ftp服務器。我的要求是檢查登錄到服務器後將顯示的路徑是否可在ftp服務器中寫入。 下面的代碼不檢查File remotefile = new File(pwd)如何檢查文件可從java寫入FTP服務器

public StringBuffer verifyMath(String host, String uname, String password, String cType){ 
    String MathString = "FTPHost:[" + host + "];uname[" + uname + "];cType[" + cType + "]"; 
    StringBuffer mBuffer = new StringBuffer(); 
    FileInputStream fis = null; 
    FTPClient client = new FTPClient(); 

    try { 
     client.connect(host); 
     boolean login = client.login(uname, password); 
     client.getReplyCode(); //230 
     if (login == true) { 
      log.debug("Connection established..."); 

      String pwd = client.printWorkingDirectory(); 
      File remotefile = new File(pwd); 
      boolean rmtfile = remotefile.canWrite(); 
      boolean rmtdir = remotefile.isDirectory(); 

      if(!(remotefile.isDirectory() && remotefile.canWrite())) { 
       mBuffer.append(MathLogger.raiseError(MathString, "Math is not Writable")); 
      }   

      boolean logout = client.logout(); 
      if (logout) { 
       log.debug("Connection close..."); 
      } 
     } else { 
      mBuffer.append(MathLogger.raiseError(MathString, "Connection failed.")); 
     } 

    } catch (IOException e) { 
     mBuffer.append(MathLogger.raiseError(MathString, e)); 
    } finally { 
     try { 
      client.disconnect(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return mBuffer; 
} 
+2

嘗試上載的東西,看看會發生什麼。 FTP協議不支持獲取讀/寫狀態。你所能做的就是試着看看它是否成功/失敗。 –

+0

受限制。因爲當文件放在FTP服務器上時,會有一些觸發器會導致其他系統出現問題 – mahesh

+0

您能否寫一些運行在FTP主機上的web服務器來返回文件的寫入狀態?正如MarcB所說,本地FTP不會傳輸這些信息。 – admdrew

回答

2

mlistFile(或可能mlistDir)可能是你正在尋找到遠程目錄調用API。返回具有權限信息的FTPFile對象。當然這些只會在FTP服務器支持RFC 3659擴展時才起作用。

因此,像:

FTPFile remoteDir = client.mlistFile(client.printWorkingDirectory()); 
if (remoteDir.hasPermission(FTPFile.USER_ACCESS,FTPFile.WRITE_PERMISSION)) { 
    ... 
} 
+0

我有Apache的公共API,但mlistFile()類型 – mahesh

+0

當我檢查的舊版本的Apache共享網沒有出現在FTPClient實例。根據文檔,它已經存在3.0以來,這是從2011年5月。 – ldav1s

+0

謝謝,我改變它的版本爲3.0,並得到了方法mlistFile()。但remoteDir返回null? client.printWorkingDirectory()將返回我列出的目錄中的所有文件被驗證爲可寫,那麼用什麼方法的許可,則它正在像/和home1/fdlqa302/CPRG/mahts/mergereR/fromGIRAR – mahesh

相關問題