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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
| package Moteur;
import java.io.*;
import java.net.SocketException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
//import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.*;
public class Ftp extends FTPClient
{
static JProgressBar progressBar;
static JLabel labelVitesse;
static JLabel labelNomFichier;
static JPanel pane;
static boolean error;
static boolean Tx;
public static int vitesse , taux;
public static String nomFichier;
/**
* @return the nomFichier
*/
public static String getNomFichier() {
return nomFichier;
}
/**
* @return the vitesse
*/
public static int getVitesse() {
return vitesse;
}
/**
* @return the taux
*/
public static int getTaux() {
return taux;
}
public Ftp()
{
}
public static JPanel getProgressBarFTP ()
{
pane = new JPanel();
labelNomFichier = new JLabel();
pane.add(labelNomFichier);
labelVitesse = new JLabel("0 Ko/s");
pane.add(labelVitesse);
//Where the GUI is constructed:
progressBar = new JProgressBar(0, 100);
pane.updateUI();
ReloadBar();
return pane;
}
public static void ReloadBar(){
threadProgress my = new Moteur.Ftp.threadProgress();
my.end = false;
my.start();
progressBar.setValue(0);
progressBar.setStringPainted(true);
pane.add(progressBar);
}
private static class threadProgress extends Thread{
boolean end = false;
public void run() {
while(!end){
final int taux = Ftp.getTaux();
if(!error){ //Si pas d'erreur
Runnable setValue = new Runnable() {
public void run() {
progressBar.setValue(taux);
}
};
SwingUtilities.invokeLater(setValue);
labelNomFichier.setText(Ftp.getNomFichier());
//progressBar.setValue(taux);
labelVitesse.setText(Integer.toString(Ftp.getVitesse())
+ "Ko/s");
//Ftp.pane.updateUI();
//System.err.println(f.getTaux());
//progressBar.setStringPainted(true);
if(taux == 100 && Tx){
System.out.println("FIN");
this.end();
labelVitesse.setText("Transfert OK");
}
}
else{
System.err.println("FIN du thread");
this.end();
//Ftp.pane.updateUI();
labelVitesse.setText("Transfert ERREUR");
}
/*
try {
this.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.err.println("Rafraichissement thread JProgressBar");
}*/
}
}
public void end(){
this.end = true;
Ftp.pane.remove(progressBar);
taux = 0;
Ftp.pane.updateUI();
progressBar.setValue(0);
}
}
public static boolean Upload(String server, String username, String password, String remote, String local)
{
FTPClient f = new FTPClient();
nomFichier = remote;
error = false;
Tx = false;
ReloadBar();
//this.addProtocolCommandListener(new PrintCommandListener( new PrintWriter(System.out))); //Active les écoutes des commandes FTP
try
{
f.connect(server);
//System.out.println("Connected to " + server + ".");
int reply = f.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
f.disconnect();
System.err.println("Connexion refusée.");
}
}
catch (IOException e)
{
if (f.isConnected())
{
try
{
f.disconnect();
}
catch (IOException g)
{
// do nothing
}
}
System.err.println("Impossible de joindre le serveur.");
//e.printStackTrace();
}
try
{
if (!f.login(username, password))
{
f.logout();
error = true;
System.out.println("Login ou mot de passe non valide");
}
//System.out.println("Remote system is " + this.getSystemName());
f.setFileType(FTP.BINARY_FILE_TYPE);
f.enterLocalPassiveMode();
File fichier = new File(local);
InputStream fis = new FileInputStream(fichier);
OutputStream os = f.storeFileStream(remote);
byte buf[] = new byte[f.getBufferSize()];
int taille=0;
long total = 0;
int bytesRead = fis.read(buf);
long temps_debut = System.currentTimeMillis();
long temps_fin = System.currentTimeMillis();
vitesse = 0;
taux = 0;
while ((taille = bytesRead) != -1) {
total+= taille;
try {
os.write(buf, 0, bytesRead);
} catch (Exception e1) {
// TODO Auto-generated catch block
error = true; // Signal l'erreur de transfert de fichier
}
bytesRead = fis.read(buf);
//System.out.println(" " + total);
temps_fin = System.currentTimeMillis();
taux = (int) ((int) total * 100 / fichier.length()) ;
try {
vitesse = (int) (( total * 1000 / 1024 ) / ( temps_fin - temps_debut ));
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
//System.out.println("Vitesse : " + vitesse);
//System.out.println("taux " + taux );
}
fis.close();
try {
os.flush();
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
if(f.completePendingCommand()){
System.out.println("Transfert completed. ");
Tx = true;
}
/*
InputStream is = new FileInputStream(local);
OutputStream os = this.storeFileStream( remote );
byte[] buffer = new byte[1024];
long total = 0;
int taille_lue = 0;
long temps_debut = System.currentTimeMillis();
long temps_fin = temps_debut;
long speed = 0;
while( ( taille_lue = is.read( buffer ) ) > 0 )
{
System.out.println("Taille " + taille_lue);
total += taille_lue;
os.write( buffer, 0, taille_lue );
temps_fin = System.currentTimeMillis();
speed = total / ( ( temps_fin - temps_debut ) / 1000 );
}
os.flush();
is.close();
os.close();
*/
// this.storeFile(remote, input);
// input.close();
f.logout();
}
catch (FTPConnectionClosedException e)
{
error = true;
System.err.println("Server closed connection.");
//e.printStackTrace();
}
catch (IOException e)
{
error = true;
//e.printStackTrace();
}
finally
{
if (f.isConnected())
{
try
{
f.disconnect();
}
catch (IOException e){}
}
}
if(error)System.err.println("C'est la crotte");
return error;
}
public boolean TestConnexion(String server) throws SocketException, IOException //Test si le serveur est joignable
{
boolean error = false;
this.connect(server);
int reply = this.getReplyCode();
if (FTPReply.isPositiveCompletion(reply))
{
this.disconnect();
System.err.println("Connexion refusée.");
}
return error;
}
public static void main(String[] args) {
for (UIManager.LookAndFeelInfo laf :
UIManager.getInstalledLookAndFeels() ){
if ("Nimbus".equals(laf.getName())) {
try {
UIManager.setLookAndFeel(laf.getClassName());
} catch (Exception e) {
// TODO: handle exception
}
}
}
JFrame frame = new JFrame();
frame.add(Ftp.getProgressBarFTP());
frame.setVisible(true);
frame.setSize(400, 400);
if(Ftp.Upload("127.0.0.1", "FTPPLS", "pls91fr", "yaya.jpg", "C:\\moteurFTP\\archive\\vrai\\TEST_20100303141929.jpg")){
System.out.println("error dude");
}
}
} |
Partager