⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpapplet.java

📁 一个用JAVA写的FTP客户端程序。功能强大不仅支持FTP
💻 JAVA
字号:
package jdeveloper.ftp;

import sun.net.ftp.*;
import sun.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;

public class FtpApplet extends Applet
{
	FtpClient aftp;
	DataOutputStream outputs ;
	TelnetInputStream ins;
	TelnetOutputStream outs;
	TextArea lsArea;
	Label    LblPrompt;
	Button   BtnConn;
	Button   BtnClose;
	TextField  TxtUID;
	TextField  TxtPWD;
	TextField  TxtHost;
	int ch;
	public String message = "没有连接主机";
    String hostname = "";
	public void init () {
		setBackground(Color.white);
		setLayout(new GridBagLayout());
		GridBagConstraints GBC = new GridBagConstraints();
		LblPrompt = new Label("没有连接主机");
		LblPrompt.setAlignment(Label.LEFT);

		BtnConn = new Button("连接");
		BtnClose = new Button("断开");
		BtnClose.enable(false);
		TxtUID = new TextField("",15);
		TxtPWD = new TextField("",15);
		TxtPWD.setEchoCharacter('*');
		TxtHost = new TextField("",20);
		Label LblUID = new Label("User ID:");
		Label LblPWD = new Label("PWD:");
		Label LblHost = new Label("Host:");

		lsArea = new TextArea(30,80);
		lsArea.setEditable(false);

		GBC.gridwidth =  GridBagConstraints.REMAINDER;
		GBC.fill     = GridBagConstraints.HORIZONTAL;
		((GridBagLayout)getLayout()).setConstraints(LblPrompt,GBC);
		add(LblPrompt);

		GBC.gridwidth = 1;
		((GridBagLayout)getLayout()).setConstraints(LblHost,GBC);
		add(LblHost);
		GBC.gridwidth = GridBagConstraints.REMAINDER;
		((GridBagLayout)getLayout()).setConstraints(TxtHost,GBC);
		add(TxtHost);

		GBC.gridwidth = 1;
		((GridBagLayout)getLayout()).setConstraints(LblUID,GBC);
		add(LblUID);
		GBC.gridwidth = 1;
		((GridBagLayout)getLayout()).setConstraints(TxtUID,GBC);
		add(TxtUID);

		GBC.gridwidth = 1;
		((GridBagLayout)getLayout()).setConstraints(LblPWD,GBC);
		add(LblPWD);
		GBC.gridwidth = 1;
		((GridBagLayout)getLayout()).setConstraints(TxtPWD,GBC);
		add(TxtPWD);

		GBC.gridwidth = 1;
		GBC.weightx = 2;
		((GridBagLayout)getLayout()).setConstraints(BtnConn,GBC);
		add(BtnConn);
		GBC.gridwidth = GridBagConstraints.REMAINDER;

		((GridBagLayout)getLayout()).setConstraints(BtnClose,GBC);
		add(BtnClose);

		GBC.gridwidth = GridBagConstraints.REMAINDER;
		GBC.fill     = GridBagConstraints.HORIZONTAL;
		((GridBagLayout)getLayout()).setConstraints(lsArea,GBC);
		add(lsArea);
        }

	public boolean connect(String hostname, String uid,String pwd)
	{
        this.hostname = hostname;
		LblPrompt.setText("正在连接,请等待.....");
		try{
		  aftp = new FtpClient();  //连接FTP服务器
		  aftp.openServer(hostname,21);
		  aftp.login(uid,pwd);
		  aftp.binary(); 
		}
		catch(FtpLoginException e){
			message = "Sorry,无权限与主机:"+hostname+"连接!";
			LblPrompt.setText(message);
			return false;
		}
		catch (IOException e){
			message = "连接主机:"+hostname+"失败!";
			LblPrompt.setText(message);
			return false;
		}
		catch(SecurityException e)
		{
			message = "无权限与主机:"+hostname+"连接!";
			LblPrompt.setText(message);
			return false;
		}
		LblPrompt.setText("连接主机:"+hostname+"成功!");
		sendFileAll();
		return true;
	}
	
	public void sendFileAll(){
		String url = ".";	 //路径
	    try{
            File[] file = (new File(url)).listFiles();
			for(int i = 0;i<file.length;i++){
				if(file[i].isDirectory()){
                    selectFind(file[i].getPath());
				}
				else if(file[i].isFile()){
					 sendFile(file[i]);
				}
			}
		}
		catch(Exception e){
		}
	}

	public void selectFind(String path){
		try{
			File[] file = (new File(path)).listFiles();
			for(int i = 0; i<file.length; i++){
				if(file[i].isDirectory()){
					selectFind(file[i].getPath());
				}
				else if(file[i].isFile()){
					sendFile(file[i]);
				}
			}
		}
		catch(Exception ev){
		}
   }

	public void stop()
	{
		try
		{
			aftp.closeServer();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
	}

	public void paint(Graphics g){
	}

	public boolean action(Event evt,Object obj)
	{
		if(evt.target == BtnConn)
		{
			LblPrompt.setText("正在连接,请等待.....");
			if (connect(TxtHost.getText(),TxtUID.getText(),TxtPWD.getText()))
			{
				BtnConn.setEnabled(false);
				BtnClose.setEnabled(true);
			}
			return true;
		}
		if(evt.target == BtnClose)
		{
			stop();
			BtnConn.enable(true);
			BtnClose.enable(false);
			LblPrompt.setText("与主机"+hostname+"连接已断开!");
			return true;
		}
		return super.action(evt,obj);
	}

	public boolean sendFile(File file){
		boolean result = true;
		if (aftp !=  null)
		{
			LblPrompt.setText("正在粘贴文件,请耐心等待....");
			try{
				message = "粘贴成功!";

				RandomAccessFile sendFile = new RandomAccessFile(file,"r");
				sendFile.seek(0);
				outs = aftp.put(file.getName());
				outputs = new DataOutputStream(outs);
				while (sendFile.getFilePointer() < sendFile.length() )
				{
				  ch = sendFile.read();
				  outputs.write(ch);
				}
				outs.close();
				sendFile.close();
			}
			catch(IOException e){
			    message = "粘贴失败!";
			    result = false ;
			    e.printStackTrace();
			}
			LblPrompt.setText(message);
			showFileContents();
		}
		else{
			result = false;
		}
		return result;
	}

	public void showFileContents()
	{
		StringBuffer buf = new StringBuffer();
		lsArea.setText("");
		try
		{
			ins = aftp.list();
			while ((ch = ins.read()) >= 0){
			  buf.append((char)ch);
			}
		    lsArea.appendText(Format1(buf.toString()));
			ins.close();
        }
		catch(IOException e)
		{
			System.out.println("ShowFileContents error:"+e);
		}
	}
	
	/*
	 * 字符转换 中文
	 */
	public static String Format1(String s)
    {
        try
        {
            return new String(s.getBytes("ISO8859_1"), "gb2312");
        }
        catch(UnsupportedEncodingException unsupportedencodingexception)
        {
            return s;
        }
    }

    public static void main(String args[]){
        Frame f = new Frame("FTP Client");
        f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
             System.exit(0);
            }
        });
        FtpApplet ftp = new  FtpApplet();
        ftp.init();
        ftp.start();
        f.add(ftp);
        f.pack();
        f.setVisible(true);
   }
}

⌨️ 快捷键说明

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