Voici ma classe Ftp :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ftp.action;
import ftp.Fenetre;
import java.io.*;
import java.net.SocketException;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.*;
/**
*
* @author D3l1
*/
public class Ftp
{
private FTPClient client;
public Ftp(String fichierLocal, String FichierSortie) throws SocketException, IOException
{
/*
* Initialisation du client FTPClient
*/
client = new FTPClient();
/*
* Activé les écoutes des commandes
*/
client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
/*
* Les élements de connexion
*/
client.connect("ftp.xxxxxxx.org");
client.login("xxxxxxx.org", "xxxxxxx");
/*
* Active le mode passive et spécifie le type de transfert binaire
*/
client.enterLocalPassiveMode();
client.setFileType(FTPClient.BINARY_FILE_TYPE);
/***********************************************************************
* Upload du fichier
**********************************************************************/
/*
* Flux binaire d'entrée
*/
InputStream fichier_local = new FileInputStream(fichierLocal);
/*
* Flux binaire de sortie
*/
OutputStream fichier_sortie = client.storeFileStream(FichierSortie);
byte[] buf = new byte[8192];
int bytesRead = fichier_local.read(buf);
while (bytesRead != -1)
{
fichier_sortie.write(buf, 0, bytesRead);
bytesRead = fichier_local.read(buf);
}
/*
* Fermeture des flux de fichiers
*/
fichier_local.close();
fichier_sortie.close();
client.completePendingCommand();
/*
* Déconnexion
*/
client.logout();
client.disconnect();
}
}
Et voici ma classe Fentre qui contient le textarea
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ftp;
import ftp.action.FileChooser;
import ftp.action.Uploader;
import java.awt.BorderLayout;
import javax.swing.*;
/**
*
* @author D3l1
*/
public class Fenetre extends JFrame
{
private JLabel labelFichierSource = new JLabel("Fichier source :");
private JButton bouttonParcourir = new JButton("Parcourir");
static JButton bouttonUploader = new JButton("Uploader");
static JTextField textBoxRepertoire = new JTextField("");
static JTextArea textareaEtat = new JTextArea();
private JProgressBar progressBar = new JProgressBar(0, 100);
public Fenetre()
{
/*
* Initialisation de la fenêtre
*/
this.setTitle("FTP Koolkiz");
this.setSize(600, 385);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
/*
* Initialisation des JPanel
*/
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
/*
* On définit le layout des JPanel
*/
pan1.setLayout(null);
pan2.setLayout(new BorderLayout());
/*
* Mise en place d'un Fieldset autour des JPanel
*/
pan1.setBorder(BorderFactory.createTitledBorder(" Choix du fichier à Uploader "));
pan2.setBorder(BorderFactory.createTitledBorder(" États du serveur "));
/*
* Placement des JPanel dans la fenêtre
*/
pan1.setBounds(10,10,565,130); // Left-Top-Width-Height
this.add(pan1);
pan2.setBounds(10,150,565,130);
this.add(pan2);
/*
* Placement des éléments dans les JPanel
*/
labelFichierSource.setBounds(40, 40, 120, 20);
pan1.add(labelFichierSource);
textBoxRepertoire.setBounds(140, 40, 380, 25);
pan1.add(textBoxRepertoire);
bouttonParcourir.addActionListener(new FileChooser());
bouttonParcourir.setBounds(140, 75, 120, 30);
pan1.add(bouttonParcourir);
bouttonUploader.setBounds(270, 75, 120, 30);
bouttonUploader.addActionListener(new Uploader());
bouttonUploader.setEnabled(false);
pan1.add(bouttonUploader);
textareaEtat.setEditable(false);
pan2.add(textareaEtat);
progressBar.setValue (0);
progressBar.setStringPainted (true);
progressBar.setBounds(10, 295, 565, 35);
this.add(progressBar);
}
public static void setRepertoire(String pChemin)
{
textBoxRepertoire.setText(pChemin);
}
public static void setUploader(boolean pEnabled)
{
bouttonUploader.setEnabled(pEnabled);
}
}