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
| package com.test.fr.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileOutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ConnexionFTP(this.getApplicationContext()).execute();
}
}
class ConnexionFTP extends AsyncTask<String, Void, String[]> {
public String modified;
public String time;
public static FTPClient mFTPClient = null;
public String TAG = "--MONERREUR--";
Context context;
final ProgressDialog pd;
public ConnexionFTP(Context applicationContext) {
// TODO Auto-generated constructor stub
this.context = applicationContext;
pd = new ProgressDialog(this.context);
}
@Override
protected void onPreExecute() {
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("download");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
protected String[] doInBackground(String... params) {
// TODO Auto-generated method stub
mFTPClient = new FTPClient();
try {
mFTPClient.connect("192.168.1.27", 21);
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
mFTPClient.login("imane", "aze");
mFTPClient.enterLocalPassiveMode();
String filePath = "db.sqlite";
time = mFTPClient.getModificationTime(filePath);
Log.d(TAG, "Server Reply: " + time);
File file = new File("/data/data/com.test.fr.test/files/MAJ.txt");
if (file.exists()) {
try (BufferedReader buff = new BufferedReader(new FileReader("/data/data/com.test.fr.test/files/MAJ.txt"))) {
String line;
line = buff.readLine();
modified = line;
Log.d(TAG, "Server Reply: " + line);
}
} else {
File myFile = new File("/data/data/com.test.fr.test/files/MAJ.txt"); //on déclare notre futur fichier
File myDir = new File("/data/data/com.test.fr.test/files"); //pour créer le repertoire dans lequel on va mettre notre fichier
Boolean success = true;
if (!myDir.exists()) {
success = myDir.mkdir(); //On crée le répertoire (s'il n'existe pas!!)
}
if (success) {
String data = "valeur lancement négatif";
FileOutputStream output = new FileOutputStream(myFile, true); //le true est pour écrire en fin de fichier, et non l'écraser
output.write(data.getBytes());
modified = data;
}
}
}
int comparaison = time.compareTo(modified);
Log.d(TAG, "output");
if (comparaison < 0) {
Log.d(TAG, "output");
try {
FileOutputStream output = context.openFileOutput("db.sqlite",
Context.MODE_PRIVATE);
Log.d(TAG, "output");
mFTPClient.retrieveFile("db.sqlite", output);
Log.d(TAG, "retrieveFile");
output.close();
mFTPClient.logout();
mFTPClient.disconnect();
FileWriter fw = new FileWriter("/data/data/com.test.fr.test/files/MAJ.txt");
fw.write(time);
fw.close();
} catch (Exception e) {
Log.d(TAG, "Erreur de téléchargement ");
e.printStackTrace();
}
} else {
mFTPClient.logout();
mFTPClient.disconnect();
}
} catch (Exception e) {
Log.d(TAG, "Erreur de connexion avec l'hote ");
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String res) {
// TODO: check this.exception
// TODO: do something with the feed
if(this.pd.isShowing())
pd.dismiss();
}
} |
Partager