ftpclient.java

来自「这是一个用JAVA写的Ftp client程序。」· Java 代码 · 共 313 行

JAVA
313
字号
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;

public class FtpClient extends JApplet implements ConnectionListener, ActionListener{
	protected FtpConnection con;
	protected boolean connected, bFileComplete;
	protected boolean bParamsSet;
	
	protected JFileChooser FileSelector;
	protected ArrayList arrList;
	protected JButton bIncreaseFile;
	protected JButton bDeleteFile;
	protected JButton bLoad;
	protected JList FileLists;
	protected Container ContPane;
	protected JProgressBar pFile, pTotal;
	protected JScrollPane sList;
	protected long lTotSize, lTotComSize; // total size in bytes
	protected long FileLstSize, lFleComSize; // total size in bytes
	
	/* those four params have to be set */
	protected String parahost;
	protected String parauser;
	protected String parapass; 
	protected String parapath; 
	
	protected String parapostscript;
	
	
	public void init() {
		
		bParamsSet = false;
		
		FileSelector = new JFileChooser();
		FileSelector.setMultiSelectionEnabled(true);
		
		bIncreaseFile = new JButton("Add File");
		bIncreaseFile.addActionListener(this);
		
		bDeleteFile = new JButton("Remove File");
		bDeleteFile.addActionListener(this);
		
		bLoad = new JButton("Upload Files");
		bLoad.addActionListener(this);
		
		pFile = new JProgressBar();
		pTotal = new JProgressBar();
		pFile.setMinimum(0);
		pFile.setMaximum(10000);
		pTotal.setMinimum(0);
		pTotal.setMaximum(10000);
	
		bFileComplete = true;
		
		FileLists = new JList();
		sList = new JScrollPane(FileLists);
		
		arrList = new ArrayList();
		
		lTotComSize = lTotSize = 
			FileLstSize = lFleComSize = 0;
	
		ContPane = this.getContPane();
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		ContPane.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(bIncreaseFile, c);
		ContPane.add(bIncreaseFile);
		
		c.weightx = 0.5;
		c.gridx = 2;
		c.gridy = 2;
		c.insets = new Insets(0,0,0,0);
		gridbag.setConstraints(bDeleteFile, c);
		ContPane.add(bDeleteFile);
		
		c.weightx = 0.5;
		c.gridx = 2;
		c.gridy = 3;
		c.insets = new Insets(0,0,0,0);
		gridbag.setConstraints(bLoad, c);
		ContPane.add(bLoad);
		
		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(sList, c);
		ContPane.add(sList);
		
		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(pFile, c);
		ContPane.add(pFile);
		
		c.gridx = 0;
		c.gridy = 6;
		c.insets = new Insets(10,0,10,0);
		c.gridheight = 1;
		c.gridwidth = 3;
		gridbag.setConstraints(pTotal, c);
		ContPane.add(pTotal);
		
		
		/* retrieve parameters */
		parahost = getParameter("host");
		parauser = getParameter("user");
		parapass = getParameter("pass"); // password
		parapath = getParameter("path"); // path on the server
		parapostscript = getParameter("postscript"); // a script to call after completion
	}

	public void start() {
		if(parahost==null || parauser==null
				|| parapass==null || parapath==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)
			lFleComSize = bytes;
		updateProgressBar();
	}
 
	public void connectionFailed(BasicConnection con, String why) 
	{System.out.println("connection failed!");}

	public void actionFinished(BasicConnection con) {
		lFleComSize = FileLstSize;
		lTotComSize += FileLstSize;
		updateProgressBar();							
		
		removeFile(0);
		updateList();
		repaint();
		
		if (!arrList.isEmpty()) {
			FileLists.setSelectedIndex(0);
			pFile.setValue(0);
			File current = (File) arrList.get(0);
			FileLstSize = current.length();
			lFleComSize = 0;
			con.handleUpload(current.getAbsolutePath());
		}
		else if(parapostscript!=null) {
			try {
				URL url = new URL(parapostscript);
				BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
			} catch(Exception e) {
				System.out.println(e.getMessage());
			}	
		}
	}
	
	protected void updateProgressBar() {
		int percent = (int) ((float)lFleComSize / (float)FileLstSize * 10000) ;
		pFile.setValue(percent);
		pFile.setString(lFleComSize/1024 + "/" + FileLstSize/1024 + " kB");
		
		percent = (int) ((float)lTotComSize /(float)lTotSize * 10000) ;
		pTotal.setString(lTotComSize/1024 + "/" + lTotSize/1024 + " kB");
		pTotal.setValue(percent);
		
		repaint();
	}
	
	public void actionPerformed(ActionEvent e) {
		// open chooser box
		if(e.getSource()==bIncreaseFile) {
			int returnVal = FileSelector.showOpenDialog(null);
			if(returnVal==JFileChooser.APPROVE_OPTION ) {
				File files[] = FileSelector.getSelectedFiles();
				addFiles(files);
				updateList();
				updateSize();
				repaint();
				FileSelector.setSelectedFile(null);
			}
		}
		else if(e.getSource()==bDeleteFile) {
			int removeidx = FileLists.getSelectedIndex();
			if(removeidx>=0)
				removeFile(removeidx);
			updateList();
			updateSize();
			repaint();
		}
		else if(e.getSource()==bLoad) {
			uploadFiles();
		}
	}
	
	protected void uploadFiles() {
		if(arrList.isEmpty())
			return;
		else if(!bParamsSet) {
			return;
		}
		
		pFile.setStringPainted(true);
		pTotal.setStringPainted(true);
		pFile.setString("0/0 kB");
		pTotal.setString("0/0 kB");
		con = new FtpConnection(parahost);
		con.addConnectionListener(this);
		ConnectionHandler handler = new ConnectionHandler();
		//con.setConnectionHandler(handler);
		con.login(parauser, parapass);
		
		while(!connected)
		{
			try { Thread.sleep(10); }
			catch(Exception ex) { ex.printStackTrace(); }
		}
		
		con.chdir(parapath);
		
		if(arrList.isEmpty())
			return;
			
		FileLists.setSelectedIndex(0);
		File current = (File) arrList.get(0);
		FileLstSize = current.length();
		lFleComSize = 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(arrList.size()==0)
			{
				arrList.add(files[i]);
				continue;
			}
			for(int j=0; j<arrList.size(); j++) {
				if( ((File)arrList.get(j)).equals(files[i]) ) {
					skip = true;
					break;
				}
			}
			
			if(!skip)
				arrList.add(files[i]);	
		}
	}
	
	protected void removeFile(int index) {
		if(index<arrList.size())
			arrList.remove(index);
	}
	
	protected void updateList() {
		int size = arrList.size();
		String[] sFileList = new String[size];
		for(int i=0; i<size; i++) {
			File temp = (File) arrList.get(i);
			sFileList[i] = temp.getName();
		}
		FileLists.setListData(sFileList);
	}
	
	protected void updateSize() {
		lTotSize = 0;
		for(int i=0; i<arrList.size(); i++) {
			File temp = (File) arrList.get(i);
			lTotSize += temp.length();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?