티스토리 뷰

Android/실전 TIP

ftp에서 파일 다운 받기

Dev-Drake 2019. 3. 25. 15:56
반응형

// 퍼미션 - 항상 이게 문제였어 ㅜㅜ 안되면 무조건 이거입니다.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
 <uses-permission android:name="android.permission.INTERNET" />

 

// 첨부파일 링크 라이브러리로 설정해야합니다.

netcomponents-1.3.8.jar

 

package pkg.ftp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

import com.oroinc.net.ftp.FTPClient;

public class FtpActivity extends Activity {
    /** Called when the activity is first created. */
 private FTPClient mFtp;
 // 폴더
 private String DIR = "/sdcard/ftp";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        CreateDir();
        GetFileFromFTP();
    }
    
    public void CreateDir() {
     File Dir = new File(DIR);
     
     Dir.mkdir();
    }
    
    public void GetFileFromFTP() { 
     if(!FTPConnect("접속할 호스트")) {
      msgBox("FTP 연결 실패 !!");
      return;
     }

     if(!FTPLogin("아이디", "패스워드")) {
      msgBox("FTP 로그인 실패 !!");
      FTPClose();
      return;
     }
     
     if(!FTPChangeDir("/ftp폴더명/")) {
      msgBox("FTP ChangeDir 실패 !!");
      FTPClose();
      return;
     }
     
     if(!FTPGetFile("111223.txt")) {
      msgBox("FTP GetFile 실패 !!");
      FTPClose();
      return;
     }
     
     msgBox("FTP 파일 다운 성공^^");
    }
    
    // FTP
    public boolean FTPConnect(String Host) {
     try {
      mFtp = new FTPClient(); 
      mFtp.connect(Host);
     }
     catch(IOException ioe){
      return false;
     }
     
     return true;
    }
    
    public boolean FTPLogin(String Id, String Pw) {
     try {
      if(!mFtp.login(Id, Pw)) return false;
     }
     catch(IOException ioe){
      return false;
     }
     
     return true;
    }
    
    public boolean FTPChangeDir(String Dir) {
     try {
      if(!mFtp.changeWorkingDirectory(Dir)) return false;
     }
     catch(IOException ioe){
      return false;
     }
      
     return true;
    }
    
    public boolean FTPClose() {
     try {
      mFtp.disconnect();
     }
     catch(IOException ioe){
      return false;
     }
      
     return true;
    }
    
    public boolean FTPGetFile(String File) {
     File f = new File(DIR, File);
     FileOutputStream os;
     
     try {
      os = new FileOutputStream(f);
      if(!mFtp.retrieveFile(File, os)) return false;
      
      os.close();
     }
     catch(IOException ioe){
      msgBox(ioe.toString());
      return false;
     }
      
     return true;
    }
    
    
    // 메시지 박스
    public void msgBox(String msg) {
        AlertDialog.Builder bld = new AlertDialog.Builder(FtpActivity.this);
        bld.setTitle("MESSAGE");
        // bld.setIcon(R.drawable.alert);
        bld.setMessage(msg);
        bld.setPositiveButton("닫 기", null);
        bld.show();
    }
}

 

반응형
댓글
반응형
최근에 올라온 글
Total
Today
Yesterday