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>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 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();}}}
ZUpload.java in ZUpload.rar ftp applet web
最新推荐文章于 2024-02-06 10:06:01 发布
ZUpload是一款基于Java Applet的文件上传工具,允许用户从本地计算机选择文件并上传到远程FTP服务器。该应用提供了进度条显示上传状态,并支持设置连接参数及上传完成后调用脚本。
2570

被折叠的 条评论
为什么被折叠?



