package com.example.ftp;
import android.util.*;
import android.app.Activity;
import org.apache.commons.net.ftp.*;
public class MainActivity extends Activity{
//Now, declare a public FTP client object.
private FTPClient mFTPClient = null;
//Method to connect to FTP server:
public boolean ftpConnect(String host, String username, String password, int port){
try {
mFTPClient = new FTPClient();
// connecting to the host
mFTPClient.connect("192.168.30.222", 21);
// now check the reply code, if positive mean connection success
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
// login using username & password
boolean status = mFTPClient.login("test", "1234");
/* Set File Transfer Mode
*
* To avoid corruption issue you must specified a correct
* transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
* EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
* for transferring text, image, and compressed files.
*/
mFTPClient.setFileType(FTPClient.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
return status;
}
} catch(Exception e) {
Log.d("TAG", "Error: could not connect to host " + host );
}
return false;
}
//Method to disconnect from FTP server:
public boolean ftpDisconnect()
{
try {
mFTPClient.logout();
mFTPClient.disconnect();
return true;
} catch (Exception e) {
Log.d("TAG", "Error occurred while disconnecting from ftp server.");
}
return false;
}
//Method to get current working directory:
public String ftpGetCurrentWorkingDirectory()
{
try {
String workingDir = mFTPClient.printWorkingDirectory();
return workingDir;
} catch(Exception e) {
Log.d("TAG", "Error: could not get current working directory.");
}
return null;
}
//Method to change working directory:
public boolean ftpChangeDirectory(String directory_path)
{
try {
return mFTPClient.changeWorkingDirectory(directory_path);
} catch(Exception e) {
Log.d("TAG", "Error: could not change directory to " + directory_path);
}
return false;
}
//Method to list all files in a directory:
public void ftpPrintFilesList(String dir_path)
{
try {
FTPFile[] ftpFiles = mFTPClient.listFiles(dir_path);
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
Log.i("TAG", "File : " + name);
}
else {
Log.i("TAG", "Directory : " + name);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
//Method to create new directory:
public boolean ftpMakeDirectory(String new_dir_path)
{
try {
boolean status = mFTPClient.makeDirectory(new_dir_path);
return status;
} catch(Exception e) {
Log.d("TAG", "Error: could not create new directory named " + new_dir_path);
}
return false;
}
//Method to delete/remove a directory:
public boolean ftpRemoveDirectory(String dir_path)
{
try {
boolean status = mFTPClient.removeDirectory(dir_path);
return status;
} catch(Exception e) {
Log.d("TAG", "Error: could not remove directory named " + dir_path);
}
return false;
}
//Method to delete a file:
public boolean ftpRemoveFile(String filePath)
{
try {
boolean status = mFTPClient.deleteFile(filePath);
return status;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
//Method to rename a file:
public boolean ftpRenameFile(String from, String to)
{
try {
boolean status = mFTPClient.rename(from, to);
return status;
} catch (Exception e) {
Log.d("TAG", "Could not rename file: " + from + " to: " + to);
}
return false;
}
//Method to download a file from FTP server:
/**
* mFTPClient: FTP client connection object (see FTP connection example)
* srcFilePath: path to the source file in FTP server
* desFilePath: path to the destination file to be saved in sdcard
*/
public boolean ftpDownload(String srcFilePath, String desFilePath)
{
boolean status = false;
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
return status;
} catch (Exception e) {
Log.d("TAG", "download failed");
}
return status;
}
//Method to upload a file to FTP server:
/**
* mFTPClient: FTP client connection object (see FTP connection example)
* srcFilePath: source file path in sdcard
* desFileName: file name to be stored in FTP server
* desDirectory: directory path where the file should be upload to
*/
public boolean ftpUpload(String srcFilePath, String desFileName, String desDirectory)
{
boolean status = false;
try {
FileInputStream srcFileStream = new FileInputStream(srcFilePath);
// change working directory to the destination directory
if (ftpChangeDirectory(desDirectory)) {
status = mFTPClient.storeFile(desFileName, srcFileStream);
}
srcFileStream.close();
return status;
} catch (Exception e) {
Log.d("TAG", "upload failed");
}
return status;
}
}
=============================================================================================================
예제 소스 보면서 오류 수정해서 완성시켰는데요..
실행하니까 그냥 하얀 화면만 나와야되는데..
어떻게해야하나요?
대충 예제소스는 탐색기 형식으로 ftp 연결하는건데 보여지는게 없네요..
대충 소스내용은 ftp 연결/로그인/파일업로드/이름바꾸기/삭제 등 이넫..
책보면서 이제 갓 안드로이드 공부하는데.. 띄엄띄엄 예제소스만보니 역시 어렵게만 느껴지네요.
아... 초보시절 아무런 개념도 탑재하지 못했을 때
게시판에 소스 왕창 붙여넣기 해서 이거 왜 안되는 거에요 하고 물어보던
내 어린시절이 생각나는군요..
그래도 전 게시판 구분할 줄은 알았습니다.