티스토리 뷰
// 퍼미션 - 항상 이게 문제였어 ㅜㅜ 안되면 무조건 이거입니다.
<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" />
// 첨부파일 링크 라이브러리로 설정해야합니다.
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();
}
}
'Android > 실전 TIP' 카테고리의 다른 글
EditText OnChange 이벤트 처리 (0) | 2019.03.25 |
---|---|
알람 통지 서비스 프로그램 (0) | 2019.03.25 |
ListView 특정 위치로 스크롤이 가게 하기 (0) | 2019.03.25 |
스마트폰의 해상도 구하기 (0) | 2019.03.25 |
서비스가 실행중인지 체크하기 (0) | 2019.03.25 |
- Total
- Today
- Yesterday