-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTPController.java
119 lines (102 loc) · 3.82 KB
/
FTPController.java
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
package edu.nctu.wirelab.testsignalv1;
import android.os.AsyncTask;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FTPController extends AsyncTask<Void, String, Integer> {
private final String TagName = "FTPController";
FTPClient ftpclient;
public boolean connectFTPServer(){
try {
ftpclient.connect("140.113.216.37", 21);
if( ftpclient.login("wirelab", "wirelab020") ){
if( !FTPReply.isPositiveCompletion(ftpclient.getReplyCode()) ){
ftpclient.disconnect();
return false;
}
}
else{
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
@Override
protected Integer doInBackground(Void... params) {
if( connectFTPServer() ){
try {
//ftpclient.makeDirectory("/file/data");
ftpclient.changeWorkingDirectory("/file/data");
ftpclient.setBufferSize(1024);
ftpclient.setControlEncoding("UTF-8");
ftpclient.enterLocalPassiveMode();
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
File folder = new File(MainActivity.LOGPATH);
String[] filelist = folder.list();
boolean showflag = true;
for( int i=0;i<filelist.length;i++ ){
if( !filelist[i].contains("uploaded") ){
if( filelist[i].contains(".sig") ){
ftpclient.changeWorkingDirectory("/file/sig");
}
else{
ftpclient.changeWorkingDirectory("/file/data");
}
FileInputStream fileInputStream = new FileInputStream(MainActivity.LOGPATH+filelist[i]);
ftpclient.storeFile(filelist[i], fileInputStream);
File thefile = new File(MainActivity.LOGPATH, filelist[i]);
thefile.renameTo(new File( MainActivity.LOGPATH, "uploaded"+filelist[i] ));
fileInputStream.close();
}
float percent = (i/(float)filelist.length)*100;
if( percent > 50 && showflag ){
publishProgress("50%");
showflag=false;
}
//Log.d(TagName, "filename: "+filelist[i]);
}
ftpclient.logout();
ftpclient.disconnect();
} catch (IOException e) {
e.printStackTrace();
//showDialog("Connection error");
return -1;
}
return 0;
}
return -1;
}
@Override
protected void onPreExecute(){
ftpclient = new FTPClient();
}
@Override
protected void onProgressUpdate(String... progress ){
ShowDialogMsg.showDialog(progress[0]);
}
@Override
protected void onPostExecute(Integer Result){
if( Result==0 )
ShowDialogMsg.showDialog("100%");
else
ShowDialogMsg.showDialog("Connection error");
}
public void removeFolder(String FolderPath){
File folder = new File(FolderPath);
String[] filelist = folder.list();
for( int i=0;i<filelist.length;i++ ){
File file = new File(FolderPath+filelist[i]);
file.delete();
}
ShowDialogMsg.showDialog("Remove 100%");
}
public void removeFile(String FilePath){
File file = new File(FilePath);
file.delete();
}
}