컴퓨터+a/컴퓨터일반

안드로이드 FTP 클라이언트/라이브러리

hiiambk 2018. 2. 1. 16:05

 

http://commons.apache.org/proper/commons-net/download_net.cgi

 

다운로드 후 압축해제

이 파일을

안드로이드 libs에 추가

(위에 목록형식을 Project로 해주어야 libs를 찾을 수 있음)

끌어다 놓으면 이런창이 뜸

OK

추가된 모습

이제 jar파일을 library로 추가

Add As Library하면 이런창이 뜨고 OK하면 추가 완료!

 

--------------------------------------------------------------------------------------------------------------------------------

 

코드

 

먼저 ConnectFTP

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package net.kccistc.iotsocketclient;
 
import android.util.Log;
 
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 java.io.FileInputStream;
import java.io.FileOutputStream;
 
/**
 * Created by HBK 2018-02-01.
 */  
public class ConnectFTP {
    private final String TAG = "Connect FTP";
    public FTPClient mFTPClient = null;
 
    public ConnectFTP(){
        mFTPClient = new FTPClient();
    }
 
    public boolean ftpConnect(String host, String username, String password, int port) {
        boolean result = false;
        try{
            mFTPClient.connect(host, port);
 
            if(FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                result = mFTPClient.login(username, password);
                mFTPClient.enterLocalPassiveMode();
            }
        }catch (Exception e){
            Log.d(TAG, "Couldn't connect to host");
        }
        return result;
    }
 
    public boolean ftpDisconnect() {
        boolean result = false;
        try {
            mFTPClient.logout();
            mFTPClient.disconnect();
            result = true;
        } catch (Exception e) {
            Log.d(TAG, "Failed to disconnect with server");
        }
        return result;
    }
 
    public String ftpGetDirectory(){
        String directory = null;
        try{
            directory = mFTPClient.printWorkingDirectory();
        } catch (Exception e){
            Log.d(TAG, "Couldn't get current directory");
        }
        return directory;
    }
 
    public boolean ftpChangeDirctory(String directory) {
        try{
            mFTPClient.changeWorkingDirectory(directory);
            return true;
        }catch (Exception e){
            Log.d(TAG, "Couldn't change the directory");
        }
        return false;
    }
 
    public String[] ftpGetFileList(String directory) {
        String[] fileList = null;
        int i = 0;
        try {
            FTPFile[] ftpFiles = mFTPClient.listFiles(directory);
            fileList = new String[ftpFiles.length];
            for(FTPFile file : ftpFiles) {
                String fileName = file.getName();
 
                if (file.isFile()) {
                    fileList[i] = "(File) " + fileName;
                } else {
                    fileList[i] = "(Directory) " + fileName;
                }
 
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileList;
    }
 
    public boolean ftpCreateDirectory(String directory) {
        boolean result = false;
        try {
            result =  mFTPClient.makeDirectory(directory);
        } catch (Exception e){
            Log.d(TAG, "Couldn't make the directory");
        }
        return result;
    }
 
    public boolean ftpDeleteDirectory(String directory) {
        boolean result = false;
        try {
            result = mFTPClient.removeDirectory(directory);
        } catch (Exception e) {
            Log.d(TAG, "Couldn't remove directory");
        }
        return result;
    }
 
    public boolean ftpDeleteFile(String file) {
        boolean result = false;
        try{
            result = mFTPClient.deleteFile(file);
        } catch (Exception e) {
            Log.d(TAG, "Couldn't remove the file");
        }
        return result;
    }
 
    public boolean ftpRenameFile(String from, String to) {
        boolean result = false;
        try {
            result = mFTPClient.rename(from, to);
        } catch (Exception e) {
            Log.d(TAG, "Couldn't rename file");
        }
        return result;
    }
 
    public boolean ftpDownloadFile(String srcFilePath, String desFilePath) {
        boolean result = false;
        try{
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
 
            FileOutputStream fos = new FileOutputStream(desFilePath);
            result = mFTPClient.retrieveFile(srcFilePath, fos);
            fos.close();
        } catch (Exception e){
            Log.d(TAG, "Download failed");
        }
        return result;
    }
 
    public boolean ftpUploadFile(String srcFilePath, String desFileName, String desDirectory) {
        boolean result = false;
        try {
            FileInputStream fis = new FileInputStream(srcFilePath);
            if(ftpChangeDirctory(desDirectory)) {
                result = mFTPClient.storeFile(desFileName, fis);
            }
            fis.close();
        } catch(Exception e){
            Log.d(TAG, "Couldn't upload the file");
        }
        return result;
    }
}
 
cs

 

fragmentFTP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package net.kccistc.iotsocketclient;
 
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 
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 java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
/**
 * Created by HBK 2018-02-01.
 */
 
public class FragmentFTP extends Fragment {
    private ConnectFTP ConnectFTP;
    final String TAG = "FragmentFTP";
    Button btnConnect;
    Button btnShow;
    TextView textViewDir;
    ImageView imageView;
    View view;
    String currentPath;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_ftp, container, false);
        ConnectFTP = new ConnectFTP();
        btnConnect = (Button) view.findViewById(R.id.button_connect);
        btnShow = (Button) view.findViewById(R.id.button_show);
        textViewDir = (TextView)view.findViewById(R.id.textViewDir);
        imageView = (ImageView)view.findViewById(R.id.imageView);
 
        btnConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean status = false;
                status = ConnectFTP.ftpConnect("192.168.30.81""ftpserver""ftpserver"21);
                if (status == true) {
                    Log.d(TAG, "Connection Success");
                } else {
                    Log.d(TAG, "Connection failed");
                }
                currentPath = ConnectFTP.ftpGetDirectory();
                textViewDir.setText(currentPath);
            }
        });
 
        btnShow.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                String newFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/FTPTest";
                File file = new File(newFilePath);
                file.mkdirs();
                newFilePath += "/test.jpg";
                try {
                    file = new File(newFilePath);
                    file.createNewFile();
                }catch (Exception e){Log.d(TAG,"fail");}
 
                ConnectFTP.ftpDownloadFile(currentPath + "/cam.jpg", newFilePath);
                //imageView.setImageDrawable();
 
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                imageView.setImageBitmap(bitmap);
 
            }
        });
        return view;
    }
 
}
 
cs

mainactivity navigation에서 fragment로

 

 

728x90