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
| package com.example.ftpthread;
import java.io.FileOutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Connexion conx=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt= (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (conx!=null){
Log.i("voila", "voila on est la 1");
conx.cancel(true);
conx=new Connexion();
conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");
}
conx=new Connexion();
conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");
}
});
}
class Connexion extends AsyncTask<String, String, String> {
FTPClient mFTPClient;
@Override
protected String doInBackground(String... params) {
Log.i("voila", "voila on est la 2");
String chaine = params[0];
try {
mFTPClient = new FTPClient();
mFTPClient.connect("site", 21);
Log.i("voila", "voila on est la 4");
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
boolean status = mFTPClient.login("user", "pass");
mFTPClient.enterLocalPassiveMode();
ftpDownload("/fromCIS/" +chaine ,
Environment.getExternalStorageDirectory()
+ "/Fromcis/" + chaine);
mFTPClient.logout();
mFTPClient.disconnect();
}
} catch (Exception e) {
}
return "zaki";
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.i("voila", "voila on est la onpost");
conx=null;
}
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(e.getCause() + "", "download failed");
}
return status;
}
}
} |
Partager