import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JApplet; import javax.swing.filechooser.*; import java.io.IOException; import java.io.File; import java.net.UnknownHostException; public class Simple extends JApplet implements ActionListener { private JFrame frame; private JFileChooser chooser; private File selected_file; private Container cp; private BorderLayout bl; private JLabel file_label; private JLabel loading_label; private JProgressBar bar; private int loaded; public void init() { chooser = new JFileChooser(); ExampleFileFilter avi_filter = new ExampleFileFilter("avi", "Avi Compressed Movie Files"); chooser.addChoosableFileFilter(avi_filter); JButton browse_btn = new JButton("Parcourir..."); browse_btn.addActionListener(this); cp = getContentPane(); bl = new BorderLayout(); cp.setLayout(bl); cp.add(browse_btn, bl.NORTH); file_label = new JLabel("Choisissez un fichier"); bar = new JProgressBar(0,99); bar.setString("Progression"); bar.setStringPainted(true); cp.add(file_label, bl.CENTER); cp.add(bar, bl.SOUTH); cp.validate(); } public void start() { } public void stop() { } public void destroy() { } public void actionPerformed(ActionEvent e) { int retval = chooser.showDialog(frame, null); if ((retval == JFileChooser.APPROVE_OPTION) && (chooser.getSelectedFile() != null)) { selected_file = chooser.getSelectedFile(); file_label.setText("Fichier : " + addSlashes(selected_file.getPath())); cp.validate(); copyFile(); } else if (retval == JFileChooser.CANCEL_OPTION) { JOptionPane.showMessageDialog(frame, "User cancelled operation. No file was chosen."); } else if (retval == JFileChooser.ERROR_OPTION) { JOptionPane.showMessageDialog(frame, "An error occured. No file was chosen."); } else { JOptionPane.showMessageDialog(frame, "Unknown operation occured."); } } private void copyFile() { Thread trd = new Thread() { FTPConnection connection = new FTPConnection(); public void run() { try { if (connection.connect("127.0.0.1")) { if (connection.login("toto", "toto")) { connection.uploadFile(selected_file.getName(), addSlashes(selected_file.getPath())); try { Thread.sleep(500); } catch (InterruptedException e) { } } } connection.disconnect(); } catch(UnknownHostException e) { } catch(IOException e) { } } }; trd.start(); while(trd.isAlive()) { FTPConnection connection2 = new FTPConnection(); try { if (connection2.connect("127.0.0.1")) { if (connection2.login("toto2", "toto2")) { loaded = (int)(connection2.getFileSize(selected_file.getName())*100/selected_file.length()); Runnable updateBar = new Runnable() { public void run() { bar.setString("Progression " + loaded + "%"); bar.setStringPainted(true); bar.setValue(loaded); cp.validate(); } }; SwingUtilities.invokeLater(updateBar); } } connection2.disconnect(); } catch(UnknownHostException e) { } catch(IOException e) { } } } private String addSlashes(String file_name) { String tmp = ""; int length = file_name.length(); int index; for (index = 0; index < length; index++) { String tmp2 = String.valueOf(file_name.charAt(index)); tmp += tmp2; if (tmp2.equals("\\")) { tmp += "\\"; } } return tmp; } }