package ZUpload; import net.sf.jftp.net.*; import java.util.ArrayList; import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.net.URL; /* * Created on Aug 25, 2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ /** * @author zhaodav * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class ZUpload extends JApplet implements ConnectionListener, ActionListener{ protected FtpConnection con; protected boolean connected, bFileComplete; protected boolean bParamsSet; protected JFileChooser fcChooser; protected ArrayList alFiles; protected JButton bAddFile; protected JButton bRemoveFile; protected JButton bUpload; protected JList lFiles; protected Container contentPane; protected JProgressBar pbFile, pbTotal; protected JScrollPane spList; protected long lTotalSize, lTotalCompleteSize; // total size in bytes protected long lFileSize, lFileCompleteSize; // total size in bytes /* those four params have to be set */ protected String param_host; protected String param_user; protected String param_pass; // password protected String param_path; // path on the server /* set this if u want to call a script after upload is complete */ protected String param_postscript; public void init() { /* create new objects */ bParamsSet = false; fcChooser = new JFileChooser(); fcChooser.setMultiSelectionEnabled(true); bAddFile = new JButton("Ajouter"); bAddFile.addActionListener(this); bRemoveFile = new JButton("Retirer"); bRemoveFile.addActionListener(this); bUpload = new JButton("Télécharger"); bUpload.addActionListener(this); pbFile = new JProgressBar(); pbTotal = new JProgressBar(); pbFile.setMinimum(0); pbFile.setMaximum(10000); pbTotal.setMinimum(0); pbTotal.setMaximum(10000); bFileComplete = true; lFiles = new JList(); spList = new JScrollPane(lFiles); alFiles = new ArrayList(); lTotalCompleteSize = lTotalSize = lFileSize = lFileCompleteSize = 0; /* set up layout */ contentPane = this.getContentPane(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); contentPane.setLayout(gridbag); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 2; c.gridy = 1; c.insets = new Insets(40,0,0,0); gridbag.setConstraints(bAddFile, c); contentPane.add(bAddFile); c.weightx = 0.5; c.gridx = 2; c.gridy = 2; c.insets = new Insets(0,0,0,0); gridbag.setConstraints(bRemoveFile, c); contentPane.add(bRemoveFile); c.weightx = 0.5; c.gridx = 2; c.gridy = 3; c.insets = new Insets(0,0,0,0); gridbag.setConstraints(bUpload, c); contentPane.add(bUpload); c.weightx = 3; c.weighty = 5; c.anchor = GridBagConstraints.NORTH; c.fill = GridBagConstraints.BOTH; c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight = 4; c.insets = new Insets(0,0,0,0); gridbag.setConstraints(spList, c); contentPane.add(spList); c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.SOUTH; c.weightx = 1; c.weighty = 0.2; c.gridx = 0; c.gridy = 5; c.insets = new Insets(10,0,10,0); c.gridheight = 1; c.gridwidth = 3; gridbag.setConstraints(pbFile, c); contentPane.add(pbFile); c.gridx = 0; c.gridy = 6; c.insets = new Insets(10,0,10,0); c.gridheight = 1; c.gridwidth = 3; gridbag.setConstraints(pbTotal, c); contentPane.add(pbTotal); /* retrieve parameters */ //param_host = getParameter("host"); //param_user = getParameter("user"); //param_pass = getParameter("pass"); // password //param_path = getParameter("path"); // path on the server //param_postscript = getParameter("postscript"); // a script to call after completion param_host = "ftp.digitalcreativ.com"; param_user = "digitalcq-pub"; param_pass = "DC Prod"; param_path = ""; param_postscript = "succes.php"; } public void start() { if(param_host==null || param_user==null || param_pass==null || param_path==null) { bParamsSet = false; } else bParamsSet = true; } public void updateRemoteDirectory(BasicConnection con) { } public void connectionInitialized(BasicConnection con) { connected = true; } public void updateProgress(String file, String type, long bytes) { if(bytes>0) lFileCompleteSize = bytes; updateProgressBar(); } public void connectionFailed(BasicConnection con, String why) {System.out.println("connection failed!");} public void actionFinished(BasicConnection con) { // when one is done, fire up another upload lFileCompleteSize = lFileSize; lTotalCompleteSize += lFileSize; updateProgressBar(); removeFile(0); updateList(); repaint(); if (!alFiles.isEmpty()) { lFiles.setSelectedIndex(0); pbFile.setValue(0); File current = (File) alFiles.get(0); lFileSize = current.length(); lFileCompleteSize = 0; con.handleUpload(current.getAbsolutePath()); } else if(param_postscript!=null) { try { URL url = new URL(param_postscript); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); } catch(Exception e) { System.out.println(e.getMessage()); } } } protected void updateProgressBar() { int percent = (int) ((float)lFileCompleteSize / (float)lFileSize * 10000) ; pbFile.setValue(percent); pbFile.setString(lFileCompleteSize/1024 + "/" + lFileSize/1024 + " kB"); percent = (int) ((float)lTotalCompleteSize /(float)lTotalSize * 10000) ; pbTotal.setString(lTotalCompleteSize/1024 + "/" + lTotalSize/1024 + " kB"); pbTotal.setValue(percent); repaint(); } public void actionPerformed(ActionEvent e) { // open chooser box if(e.getSource()==bAddFile) { int returnVal = fcChooser.showOpenDialog(null); if(returnVal==JFileChooser.APPROVE_OPTION ) { File files[] = fcChooser.getSelectedFiles(); addFiles(files); updateList(); updateSize(); repaint(); fcChooser.setSelectedFile(null); } } else if(e.getSource()==bRemoveFile) { int removeidx = lFiles.getSelectedIndex(); if(removeidx>=0) removeFile(removeidx); updateList(); updateSize(); repaint(); } else if(e.getSource()==bUpload) { uploadFiles(); } } protected void uploadFiles() { if(alFiles.isEmpty()) return; else if(!bParamsSet) { return; } pbFile.setStringPainted(true); pbTotal.setStringPainted(true); pbFile.setString("0/0 kB"); pbTotal.setString("0/0 kB"); con = new FtpConnection(param_host); con.addConnectionListener(this); ConnectionHandler handler = new ConnectionHandler(); //con.setConnectionHandler(handler); con.login(param_user, param_pass); while(!connected) { try { Thread.sleep(10); } catch(Exception ex) { ex.printStackTrace(); } } con.chdir(param_path); if(alFiles.isEmpty()) return; lFiles.setSelectedIndex(0); File current = (File) alFiles.get(0); lFileSize = current.length(); lFileCompleteSize = 0; bFileComplete = false; con.handleUpload(current.getAbsolutePath()); } protected void addFiles(File[] files) { boolean skip = false; for(int i=0; i