2014-02-20 2 views
0

FTP 서버에 연결할 수 있습니다. 내 요구 사항은 서버에 로그인 한 후 표시 될 경로가 ftp 서버에서 쓰기 가능한지 확인하는 것입니다. 코드 아래 이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 호스트에서 실행중인 일종의 웹 서비스를 작성하여 파일의 쓰기 상태를 반환 할 수 있습니까? MarcB가 말했듯이, 원시 FTP는 해당 정보를 전송하지 않습니다. – admdrew

답변

2

mlistFileFile remotefile = new File(pwd) 확인되지 않은 (또는 아마도 mlistDir가) 아마 당신이 원격 디렉토리에 전화를 할 찾고있는 API입니다. 그러면 권한 정보가있는 FTPFile 개체가 반환됩니다. 물론 FTP 서버가 RFC 3659 확장을 지원하는 경우에만 작동합니다.

그래서 같은 :

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

아파치 commons API가 있지만 mlistFile()이 FTPClient 인스턴스에 나타나지 않습니다. – mahesh

+0

Apache Commons Net의 이전 버전을 확인합니다. 문서에 따르면, 2011 년 5 월부터 3.0이되었습니다. – ldav1s

+0

덕분에 3.0 버전으로 바뀌었고 mlistFile() 메서드가 생겼습니다. 그러나 remoteDir은 null을 리턴하고 있습니까? client.printWorkingDirectory()는/home1/fdlqa302/cprg/mahts/mergereR/fromGIRAR와 같은 경로를 반환합니다. – mahesh