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
| import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.FileOutputStream;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// faire appell à la classe interne pour l'executer
new ConnexionFTP().execute();
}
}// FIN MAIN ACTIVITY
class ConnexionFTP extends AsyncTask<String, Void, String[]> {
public static FTPClient mFTPClient = null;
public String TAG = "--MONERREUR--";
Context context;
String fileName = "bdd.sqlite";
File mFile = null;
boolean status = false;
boolean result = false;
void ConnextionFTP(Context co) {
context = co;
}
@Override
protected String[] doInBackground(String... params) {
// TODO Auto-generated method stub
BufferedOutputStream fos = null;
mFTPClient = new FTPClient();
try {
// connexion à l'hôte
mFTPClient.connect("192.168.1.81", 21);
// test de la connexion
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
// SE CONNECTER avec login et mdp
boolean status = mFTPClient.login("jojo", "toto");
mFTPClient.enterLocalPassiveMode();
}
/** Pour télécharger le fichier
*
*/
try{
String destpath="bdd.sqlite";
String destname="bdd.sqlite";
File pathSpec = new File(destpath);
Log.d(TAG, "new file");
mFTPClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
//fos = new BufferedOutputStream(new FileOutputStream(pathSpec.toString()+"/"+destname));
Log.d(TAG, "setfiletype"+Environment.getExternalStorageDirectory().getName());
//
//FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getName()+"/Download/bdd.sqlite");//destination du fichier
FileOutputStream output = null;
output = context.openFileOutput(Environment.getExternalStorageDirectory().getName()+"/Download/bdd.sqlite",context.MODE_PRIVATE);
Log.d(TAG, "output");
mFTPClient.retrieveFile("/bdd.sqlite", output);
Log.d(TAG, "retrieveFile");
Log.d(TAG, "téléchargement TRY ");
//déconnection
output.close();
mFTPClient.logout();
mFTPClient.disconnect();
} catch (Exception e) {
Log.d(TAG, "Erreur de téléchargement ");
}
} catch (Exception e) {
Log.d(TAG, "Erreur de connexion avec l'hote ");
}
return null;
}
} |
Partager