2014-07-07 8 views
5

FTP 서버에서 안드로이드 장치으로 이미지를 다운로드하려면 요구 사항이 입니다. ftp4j-1.7.2.jar 라이브러리를 사용하여 약간의 샘플로 시도했지만 은 FTP 서버와 연결하지 못했습니다. &이 엉망입니다.FTP 서버에서 Android 장치로 파일을 다운로드하는 방법은 무엇입니까?

FTP 서버로 작업 한 사람이 있습니까?

서버에 & 다운로드 파일을 연결하도록 제안하십시오. 코드 아래

답변

10

를 사용하여 라이브러리 commons.apache.org/proper/commons-net

확인이 FTP 서버에서 파일을 다운로드 :

private Boolean downloadAndSaveFile(String server, int portNumber, 
    String user, String password, String filename, File localFile) 
    throws IOException { 
FTPClient ftp = null; 

try { 
    ftp = new FTPClient(); 
    ftp.connect(server, portNumber); 
    Log.d(LOG_TAG, "Connected. Reply: " + ftp.getReplyString()); 

    ftp.login(user, password); 
    Log.d(LOG_TAG, "Logged in"); 
    ftp.setFileType(FTP.BINARY_FILE_TYPE); 
    Log.d(LOG_TAG, "Downloading"); 
    ftp.enterLocalPassiveMode(); 

    OutputStream outputStream = null; 
    boolean success = false; 
    try { 
     outputStream = new BufferedOutputStream(new FileOutputStream(
       localFile)); 
     success = ftp.retrieveFile(filename, outputStream); 
    } finally { 
     if (outputStream != null) { 
      outputStream.close(); 
     } 
    } 

    return success; 
} finally { 
    if (ftp != null) { 
     ftp.logout(); 
     ftp.disconnect(); 
    } 
} 
} 
+0

안녕, 내 바탕 화면에서 FileZilla의 FTP 서버를 설치했습니다. 그것의 주소는 127.0.0.1이고, 포트는 14147입니다. UserName, Password, fileName, localfile에 무엇을 주어야합니까 ?? –

+0

이 링크를 사용하여 http://lifehacker.com/339887/build-a-home-ftp-server-with-filezilla를 사용하여 filezilla로 사용자를 만들고 & 사용 방법을 알 수 있습니다. 또는 동일한 설정을 확인하십시오 http://www.addictivetips.com/windows-tips/how-to-setup-personal-ftp-server-using-filezilla-step-by-step-guide/ –

+0

도서관은 무엇입니까? ??? 나는 org.apache.commons.net.ftp.FTP.sendCommand (FTP.java:471)에서 java.lang.NullPointerException을 얻고있다. –

관련 문제