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

📄 ftpapplet.java

📁 用JAVA编写的FTP客户端程序代码
💻 JAVA
字号:
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;
    FileOutputStream fos;
	FileInputStream fis;
    TelnetInputStream ins;
    TelnetOutputStream outs;
    TextArea lsArea;
    Label    LblPrompt;
    Button   BtnConn;
    Button   BtnClose;
	Button   BtnDownload;
	Button   BtnUpload;
	Button   BtnCd;
    TextField  TxtUID;
    TextField  TxtPWD;
    TextField  TxtHost;
	TextField  TxtLocalFile;
	TextField  TxtRemoteFile;
	TextField  TxtPath;
    int ch;
    public String a="没有连接主机";
    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); 
		BtnDownload = new Button("下载");
		BtnUpload = new Button("上传");
		BtnCd = new Button("CD");
		
		TxtUID = new TextField("",15); 
		TxtPWD = new TextField("",15); 
		TxtPWD.setEchoCharacter('*'); 
		TxtHost = new TextField("",20);
		TxtLocalFile = new TextField("",20);
		TxtRemoteFile = new TextField("",20);
		TxtPath = new TextField("",15);
		
		Label LblUID = new Label("User ID:"); 
		Label LblPWD = new Label("PWD:"); 
		Label LblHost = new Label("Host:"); 
		Label LblLocalFile = new Label("LocalFileName:");
		Label LblRemoteFile = new Label("RemoteFileName:");
		Label LblPath = new Label("Path:");
		
		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=1;
		((GridBagLayout)getLayout()).setConstraints(LblLocalFile,GBC);
		add(LblLocalFile);
		GBC.gridwidth=GridBagConstraints.REMAINDER;
		((GridBagLayout)getLayout()).setConstraints(TxtLocalFile,GBC);
		add(TxtLocalFile);
		
		GBC.gridwidth=1;
		((GridBagLayout)getLayout()).setConstraints(LblRemoteFile,GBC);
		add(LblRemoteFile);
		GBC.gridwidth=GridBagConstraints.REMAINDER;
		((GridBagLayout)getLayout()).setConstraints(TxtRemoteFile,GBC);
		add(TxtRemoteFile);
		
		GBC.gridwidth=1;
		((GridBagLayout)getLayout()).setConstraints(LblPath,GBC);
		add(LblPath);
		GBC.gridwidth=1;
		((GridBagLayout)getLayout()).setConstraints(TxtPath,GBC);
		add(TxtPath);
		GBC.gridwidth=1;
		GBC.weightx=2;
		((GridBagLayout)getLayout()).setConstraints(BtnDownload,GBC);
		add(BtnDownload);
		GBC.gridwidth=1;
		((GridBagLayout)getLayout()).setConstraints(BtnUpload,GBC);
		add(BtnUpload);
		GBC.gridwidth=GridBagConstraints.REMAINDER;
		((GridBagLayout)getLayout()).setConstraints(BtnCd,GBC);
		add(BtnCd);
		
		GBC.gridwidth=GridBagConstraints.REMAINDER;
		GBC.fill     = GridBagConstraints.HORIZONTAL;
		((GridBagLayout)getLayout()).setConstraints(lsArea,GBC);
		add(lsArea); 
	}

    public boolean connect(String hostname, String uid,String pwd) {   //与FTP服务器连接,由连接按钮BtnConn上的单击触发
		this.hostname = hostname;
		LblPrompt.setText("正在连接,请等待.....");
		
		try {
			aftp =new FtpClient(hostname);
			aftp.login(uid,pwd);
			aftp.binary();
			showFileContents();
		} catch(FtpLoginException e){ 
			a="无权限与主机:"+hostname+"连接!";
			LblPrompt.setText(a);
			return false;
		} catch (IOException e){
			a="连接主机:"+hostname+"失败!";
			LblPrompt.setText(a);
			return false;
		} catch(SecurityException e) {
			a="无权限与主机:"+hostname+"连接!";
			LblPrompt.setText(a);
			return false;
		}
		LblPrompt.setText("连接主机:"+hostname+"成功!");
		return true;
	}

    public void stop() {         //与FTP服务器断开连接,由断开按钮BtnClose上的单击触发
		try { 
			aftp.closeServer();
		} catch(IOException e) {
		}
	}

    public void paint(Graphics g){ 
    }

    public boolean action(Event evt,Object obj) {     //事件处理
		if (evt.target == BtnConn) {       //连接事件处理-->connect(TxtHost.getText(),TxtUID.getText(),TxtPWD.getText())
			LblPrompt.setText("正在连接,请等待.....");
			if(connect(TxtHost.getText(),TxtUID.getText(),TxtPWD.getText())){
				BtnConn.setEnabled(false);
				BtnClose.setEnabled(true);
			}
			return true;
		}
		if (evt.target == BtnClose) {      //断开事件处理-->stop()-->aftp.closeServer()
			stop();
			BtnConn.enable(true);
			BtnClose.enable(false);
			LblPrompt.setText("与主机"+hostname+"连接已断开!");
			return true;
		}
		if (evt.target == BtnCd) {         //切换目录事件处理-->aftp.cd(TxtPath.getText())
			try {
				aftp.cd(TxtPath.getText());
				showFileContents();
			}catch (IOException e) {
			}
			return true;
		}
		if (evt.target == BtnDownload) {    //下载文件事件处理
			try {
				ins = aftp.get(TxtRemoteFile.getText());
				File outfile = new File(TxtLocalFile.getText());
				fos = new FileOutputStream(outfile);
				byte[] bytes = new byte[1024];
				while ((ch = ins.read(bytes)) != -1) {
					fos.write(bytes, 0, ch);
				}
			}catch (IOException e){
				e.printStackTrace();
			}
			try {
				if (ins != null) {
					ins.close();
				}
				if (fos != null) {
					fos.close();
				}
			}catch (IOException e){
			}
			LblPrompt.setText("文件"+TxtRemoteFile.getText()+"已经传到本地");
			return true;
		}
		if (evt.target == BtnUpload) {     //上传文件事件处理
			try {
				File file_in = new File(TxtLocalFile.getText());
				outs = aftp.put(TxtRemoteFile.getText());
				fis = new FileInputStream(file_in);
				byte[] bytes = new byte[1024];
				while ((ch = fis.read(bytes)) != -1) {
					outs.write(bytes, 0, ch);
				}
			}catch (IOException e){
				e.printStackTrace();
			}
			try {
				if (fis != null) {
					fis.close();
				}
				if (outs != null) {
					outs.close();
				}
			}catch (IOException e){
			}
			LblPrompt.setText("文件"+TxtLocalFile.getText()+"已经传到服务器");
			return true;
		}
		return super.action(evt,obj);
	}

    public void showFileContents() {   //显示服务器当前目录的文件及文件夹列表
		StringBuffer buf = new StringBuffer();
		lsArea.setText("");
		try {
			ins= aftp.list();
			while ((ch=ins.read())>=0){
				buf.append((char)ch);
			}
			lsArea.appendText(buf.toString());
			ins.close();
		} catch(IOException e) {
		}
	}
	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 + -