2015-01-22 5 views
2

FTP 서버에서 파일을 다운로드하는 Java 기능을 만들었습니다. 그것은 내 로컬 컴퓨터에서 잘 작동합니다. 하지만 리눅스 서버 (다른 호스트와 포트를 의미)에서 실행해야합니다. 그리고 함수가FTP 서버에서 파일을 다운로드 할 수 없습니다.

The collection, array, map, iterator, or enumeration portion of a for statement cannot be null 

코드와 라인에서 발생하는 오류 제공 :

for(String f : ftpNames) { 
    ftpclient.retrieveFile(f, os); // os is OutputStream 
} 

그래서이 파일을 참조하지 않습니다 ... 나는

ftpclient.enterRemotePassiveMode(); 

그리고 추가 ftpclient.getPassiveHost()227 Entering Passive Mode (x,x,x,x,204,15)을 반환합니다.

목록을 표시하고 다운로드하려고 시도했습니다. 을 통해 작동합니다.

문제를 해결하기 위해 코드를 어떻게 수정해야합니까? 감사.

UPD. 내가 가진 내가에서 파일을 얻으려고 FTP 서버에서 로그와 같은 문자열이 :

425 Cannot open data connection 

전체 코드 : 오류 메시지가 설명

static boolean ftpFilesDownload(String ip, int port, String login, String passwd, String ftpdir, String localdir) throws IOException { 
     Boolean result = false; 

     FTPClient client = new FTPClient(); 
     String separator = File.separator; 


     try {        
      client.connect(ip, port);  
      System.out.println(client.getReplyString()); 
      client.login(login, passwd); 
      System.out.println(client.getReplyString()); 

      client.setControlKeepAliveTimeout(1000*60*5); 
      client.setControlKeepAliveReplyTimeout(1000*60*5); 
      client.setFileType(FTP.BINARY_FILE_TYPE); 
      System.out.println("client setFileType success");  

      client.changeWorkingDirectory(ftpdir); 
      System.out.println(client.getReplyString()); 
      client.printWorkingDirectory(); 
      System.out.println("directory changed");         

      FTPFile[] ftpFiles = client.listFiles(); 
      System.out.println(ftpFiles);       

      String[] ftpNames = client.listNames(); 
      System.out.println("the files are " + Arrays.toString(ftpNames)); // so null here... 
       for(String f : ftpNames) {     
      String localfile = localdir + f; 
      OutputStream os = new FileOutputStream(localfile); 
      try { 
       result = client.retrieveFile(f, os); 
       System.out.println("DOWNLOADING STARTED); 
       System.out.println(client.getReplyString()); 
       client.noop(); 
      } 
      catch(Exception e) { 
       System.out.println(e);      
       result = false; 
      } 
      finally { 
       if(os != null) 
        os.close(); 
       } 
      } 
      client.logout(); 
      System.out.println(client.getReplyString()); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
      result = false; 
     } 
     finally 
     { 
      try 
      { 
       client.disconnect(); 
      } 
      catch(Exception e) 
      { 
       System.out.println(e); 
       result = false; 
      } 
     } 
     return result; 
    } 
+0

어떻게 'ftpNames' 값을 설정하고 있습니까? 그것이 바로 불평하는 것입니다. –

+0

'String [] ftpNames = ftpclient.listNames();' – Daria

+1

안드로이드 앱을 통해 링크 할 수 없지만 Google에서 "java ftpclient listfiles null"을 검색하면 유용하다고 생각되는 stackoverflow 응답이 나타났습니다. "java - org.apache.commons.net.ftp.FTPClient listFiles 문제". 단일 표준이 없으므로 특정 항목 구문 분석기를 지정해야한다고 제안합니다. –

답변

0

, 당신은 반복하려는은 null 개체 이상 당신이 확인 (또는 확인 빈의 Iterable 아마 사용하기)

을이이 execptional (오류) 상태 인 경우, 내가 명시 적으로 확인 거라고 및 런타임 예외의 어떤 종류를 발생한다 예 :

if (ftpNames == null) { 
    throw new IllegalArgumentException("Cannot use a null set of FTP servers"); 
} 

for (String f : ftpNames) { 
    ftpclient.retrieveFile(f, os); // os is OutputStream 
} 

또는 FTP 서버를 계속 사용하지 않아도되지만 약간 의미가 없습니다.

+0

'ftpNames'가 null이라는 것을 알 수 있습니다. 무엇이 원인인지 알아낼 수 없습니다. – Daria

+0

ftpclient의 세부 정보 (즉, 사용중인 FTP 클라이언트 라이브러리)를 추가 할 수 있습니까? –

+0

내 질문에 전체 코드를 게시했습니다 – Daria

-1

ftpclient.enterLocalActiveMode();

관련 문제