posted by Solver Full-stack Developer 2011.10.17 17:27
Step 1. Add externer archives...
-u r project -> Rclick -> build path ->add externer archives... 


Step 2. Source
package com.xxx.xxx;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

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;

import android.util.Log;


public class FTPUtill {
String SERVERIP = "127.0.0.1";
int PORT = 21;
String SERVERID = "u r id";
String SERVERPW = "u r pw";
    FTPClient ftpClient=null;

boolean DownloadContents(String filename){
this.ftpClient = new FTPClient();
        connect();
login(SERVERID,SERVERPW);
  cd("Root/xxx");//input u r directory
  FTPFile[] files = list();
  if(files==null){
  return false;
  }
  ArrayList<String> ImageIds_tmp = new ArrayList<String>();
  for(int i =0 ;i<files.length;i++){
String fileName = files[i].getName();
            String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
            long size = files[i].getSize();
            extension=extension.toUpperCase();            
if (size > 0) {
for(int j=0;j<size;j++){
if(filename.equalsIgnoreCase(fileName.substring(0, fileName.indexOf(".")))){
StringBuffer furl = new StringBuffer("/sdcard/xxx/");
furl.append(fileName);
ImageIds_tmp.add(furl.toString());
get(fileName, fileName);
}
}
}         
logout();
        disconnect();
        return true;
}
    public boolean login(String user, String password) {
        try {
            this.connect();
            return this.ftpClient.login(user, password);
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return false;
    }

    private boolean logout() {
        try {
            return this.ftpClient.logout();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return false;
    }

    public void connect() {
        try {
        this.ftpClient.connect(SERVERIP, PORT);
            int reply;
            reply = this.ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(reply)) {
            this.ftpClient.disconnect();
            }
        }
        catch (IOException ioe) {
            if(this.ftpClient.isConnected()) {
                try {
                this.ftpClient.disconnect();
                } catch(IOException f) {;}
            }
        } 
    }

    public FTPFile[] list() {
        FTPFile[] files = null;
        try {
            files = this.ftpClient.listFiles();
            return files;
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }

    public File get(String source, String target) {
        OutputStream output = null;
        try {
        StringBuffer furl = new StringBuffer("/sdcard/xxx/");
        File path = new File(furl.toString());
            if(! path.isDirectory()) {
                    path.mkdirs();
              }
            
            furl.append(target);
            File local = new File(furl.toString());
            if(local.isFile()){
            return null;
            }
            output = new FileOutputStream(local);
        }
        catch (FileNotFoundException fnfe) {;}
        File file = new File(source);
        try {
            if (this.ftpClient.retrieveFile(source, output)) {
                return file;
            }
        }
        catch (IOException ioe) {;}
        return null;
    }

    public void cd(String path) {
        try {
        this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        this.ftpClient.enterLocalPassiveMode();
        this.ftpClient.changeWorkingDirectory(path);
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    private void disconnect() {
        try {
        this.ftpClient.disconnect();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

안녕하세요 ^^ 
잠시 설명을 약간 해볼까해요.
전 아파치에서 제공하는 FTP를 사용하였는데요.
위에 소스를 잘 분석하시면 사용방법은 간단합니다.
서버 ip 사용자 id, pw, port를 설정해주시고
맨위쪽에 DownloadContents부분을 하나씩 차근차근 디버깅하시면서 
보시면 이해가 되실꺼에요.
접속하고 디렉토리로 이동하고 파일리스트 가져와 내가원하는 파일 찾아서 다운로드!!

그럼 빠른 업무효율을 위해 DownloadContents ctlr+f ㄱㄱ싱

도움이 되셨길 빌께요 ^^

즐코딩! 낫빡침! 

신고

티스토리 툴바