ZUpload.java in ZUpload.rar ftp applet web

ZUpload是一款基于Java Applet的文件上传工具,允许用户从本地计算机选择文件并上传到远程FTP服务器。该应用提供了进度条显示上传状态,并支持设置连接参数及上传完成后调用脚本。
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 bytesprotected 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; // passwordprotected 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"); // passwordparam_path = getParameter("path"); // path on the serverparam_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;}elsebParamsSet = 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 uploadlFileCompleteSize = 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 boxif(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())alFiles.remove(index);}protected void updateList() {int size = alFiles.size();String[] sFileList = new String[size];for(int i=0; i<size; i++) {File temp = (File) alFiles.get(i);sFileList[i] = temp.getName();}lFiles.setListData(sFileList);}protected void updateSize() {lTotalSize = 0;for(int i=0; i<alFiles.size(); i++) {File temp = (File) alFiles.get(i);lTotalSize += temp.length();}}}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值