• PUDN用户
    了解作者
  • Java
    开发工具
  • 462KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 35
    下载次数
  • 2005-08-05 21:47
    上传日期
老外写的用来FTP的APPLET是WEB模式FTP不可缺少的利器
ZUpload.rar
内容介绍
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; import netscape.javascript.JSObject; import netscape.javascript.JSException; /* * Created on Aug 25, 2003 * * To change the template for this generated file go to * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments */ /** * @author zhaodav * * To change the template for this generated type comment go to * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;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("浏览"); // bAddFile.addActionListener(this); // bRemoveFile = new JButton("Remove File"); // bRemoveFile.addActionListener(this); bUpload = new JButton("Upload Files"); 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 } 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<files.length; i++) { skip = false; if(alFiles.size()==0) { alFiles.add(files[i]); continue; } for(int j=0; j<alFiles.size(); j++) { if( ((File)alFiles.get(j)).equals(files[i]) ) { skip = true; break; } } if(!skip) alFiles.add(files[i]); } } protected void removeFile(int index) { if(index<alFiles.size()) alFil
评论
    相关推荐
    • Applet通过FTP上传大文件
      一个applet小应用城西,通过commons-net包操作ftp上传大文件,有上传进度显示。上传完成后可以回传文件名和文件路径给js,网页参数设置如下: <applet code="com.cmp.training.ftp.FtpJApplet.class" name="ftp...
    • Java Applet FTP上传
      一个Java Applet小程序。提供FTP上传文件(目录),支持拖放本地文件。可配置上传文件的格式等。 测试配置:Tomcat6.0 (IE控件) FTP服务端:TYPSoft FTP Server Client
    • ftp client applet
      NULL 博文链接:https://wuzijingaip.iteye.com/blog/1173936
    • Java实现ftp功能
      实现ftp上传下载服务器端代码。需要大于20个需要大于20个
    • ftp client applet
      NULL 博文链接:https://wuzijingaip.iteye.com/blog/1173936
    • applet ftp client 例子
      applet ftp client 例子
    • ftp client applet
      NULL 博文链接:https://wuzijingaip.iteye.com/blog/1173936
    • JFTP &FTP$applet
      功能很强大,源码开放,有兴趣的可以研究一下,本人也是从网上下载的,还没有去研究,希望对大家有所帮助
    • java FTP客户端程序
      用java 写的ftp客户端程序,个人觉得是写得比较清晰的
    • ftp client applet
      NULL 博文链接:https://wuzijingaip.iteye.com/blog/1173936