FTP网络文件传输

      需要用到JAVA API提供的FtpClient,TelnetInputStream,RandomAccessFile等几个类. (1)FtpClient类 负责客户端与服务器端的通讯,实现了客户端FTP传输的功能. (2)TelnetInputStream类 负责辅助完成FTP的传输,是一种输入流类 (3)RandomAccessFile类 文件随机访问 下面实现一个FTP客户端程序,用来下载FTP服务端的文件,实现步骤如下: (1)输入服务器名以及用于登录的用户名和密码,登录FTP服务器 (2)登录成功后,列出服务器端的目录和文件名 (3)选择要下载的文件和存储的本机目录完成文件下载 创建一个包含main()方法的客户端FtpClientFrame类
package ftpclient;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import sun.net.ftp.*;

import sun.net.*;

import java.io.*;



public class FtpClientFrame extends JFrame{

	JPanel contentPane;

	Label labelPrompt=new Label();

	Label labelHost=new Label();

	TextField textFieldHost=new TextField();

	Label labelUser=new Label();

	TextField textFieldUser=new TextField();

	Label labelPassword=new Label();

	TextField textFieldPassword=new TextField();

	Button buttonLink=new Button();

	Button buttonDisconnect=new Button();

	Label labelFileShow=new Label();

	TextArea textAreaContent=new TextArea();

	Label labelFile=new Label();

	TextField textFieldFile=new TextField();

	Label labelDir=new Label();

	TextField textFieldDir=new TextField();

	Button buttonDownload=new Button();

	FtpClient myFtp=null;

	TelnetInputStream inStream=null;

	

	public FtpClientFrame(){

		try{

			jbInit();

		}

		catch(Exception e){

			e.printStackTrace();

		}

	}

	

	private void jbInit() throws Exception{

		contentPane=(JPanel)this.getContentPane();

		contentPane.setLayout(null);

		labelPrompt.setBounds(new Rectangle(25,6,180,22));

		labelHost.setText("主机名");

		labelHost.setBounds(new Rectangle(25,38,50,22));

		textFieldHost.setBounds(new Rectangle(78,38,280,22));

		labelUser.setText("用户名");

		labelUser.setBounds(new Rectangle(25,70,50,22));

		textFieldUser.setBounds(new Rectangle(78,70,114,22));

		labelPassword.setText("密码:");

		labelPassword.setBounds(new Rectangle(205,70,37,22));

		textFieldPassword.setBounds(new Rectangle(244,70,114,22));

		textFieldPassword.setEchoChar('*');

		buttonLink.setLabel("连接");

		buttonLink.setBounds(new Rectangle(375,38,70,22));

		buttonLink.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){

				buttonLink_actionPerformed(e);

			}

		});

		buttonLink.setEnabled(true);

		buttonDisconnect.setLabel("断开");

		buttonDisconnect.setBounds(new Rectangle(375,70,70,22));

		buttonDisconnect.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){

				buttonDisconnect_actionPerformed(e);

			}

		});

		buttonDisconnect.setEnabled(false);

		labelFileShow.setText("目录列表");

		labelFileShow.setBounds(new Rectangle(25,105,140,22));

		textAreaContent.setBounds(new Rectangle(25,135,420,235));

		textAreaContent.setEditable(false);

		labelFile.setText("欲下载的文件");

		labelFile.setBounds(new Rectangle(25,380,100,22));

		textFieldFile.setBounds(new Rectangle(128,380,230,22));

		labelDir.setText("存放文件的路径");

		labelDir.setBounds(new Rectangle(25,412,100,22));

		textFieldDir.setBounds(new Rectangle(1285,412,230,22));

		buttonDownload.setLabel("下载");

		buttonDownload.setBounds(new Rectangle(375,412,70,22));

		buttonDownload.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){

				buttonDownload_actionPerformed(e);

			}

		});

		buttonDownload.setEnabled(false);

		contentPane.add(labelPrompt,null);

		contentPane.add(labelHost,null);

		contentPane.add(textFieldHost,null);

		contentPane.add(labelUser,null);

		contentPane.add(textFieldUser,null);

		contentPane.add(labelPassword,null);

		contentPane.add(textFieldPassword,null);

		contentPane.add(buttonLink,null);

		contentPane.add(buttonDisconnect,null);

		contentPane.add(labelFileShow,null);

		contentPane.add(textAreaContent,null);

		contentPane.add(textFieldFile,null);

		contentPane.add(labelFile,null);

		contentPane.add(labelDir,null);

		contentPane.add(textFieldDir,null);

		contentPane.add(buttonDownload,null);

		enableEvents(AWTEvent.WINDOW_EVENT_MASK);

		this.setSize(new Dimension(480,485));

		this.setResizable(false);

		this.setTitle("ftp客户端");

		this.setVisible(true);

	}

	

	void buttonLink_actionPerformed(ActionEvent e){

		String hostname=textFieldHost.getText();

		labelPrompt.setText("正在连接...");

		try{

			myFtp=new FtpClient(hostname);

			myFtp.login(textFieldUser.getText(), textFieldPassword.getText());

			myFtp.binary();

			showFileContents();

		}

		catch(FtpLoginException e1){

			String strPrompt="用户名密码错误";

			labelPrompt.setText(strPrompt);

		}

		catch(IOException e1){

			String strPrompt="连接主机"+hostname+"失败";

			labelPrompt.setText(strPrompt);

		}

		catch(SecurityException e1){

			String strPrompt="无权限与主机:"+hostname+"连接";

			labelPrompt.setText(strPrompt);

		}

		labelPrompt.setText("连接主机:"+hostname+"成功");

		buttonDisconnect.setEnabled(true);

		buttonDownload.setEnabled(true);

		buttonLink.setEnabled(false);

	}

	

	void buttonDisconnect_actionPerformed(ActionEvent e){

		try{

			myFtp.closeServer();

			textAreaContent.setText("");

			labelPrompt.setText("与主机断开");

		}

		catch(IOException e1){

			System.out.println("Error: "+e1);

		}

		buttonLink.setEnabled(true);

		buttonDownload.setEnabled(false);

		buttonDisconnect.setEnabled(false);

	}

	

	public void showFileContents(){

		int ch;

		StringBuffer  buf=new StringBuffer();

		try{

			inStream=myFtp.list();

			while((ch=inStream.read())>=0){

				buf.append((char)ch);

			}

			textAreaContent.append(buf.toString());

			inStream.close();

		}

		catch(Exception e){

			System.out.println("Error: "+e);

		}

	}

	

	void buttonDownload_actionPerformed(ActionEvent e){

		int ch;

		StringBuffer buf=new StringBuffer();

		buf.setLength(0);

		try{

			File dir=new File(textFieldDir.getText());

			File f=new File(dir,textFieldFile.getText());

			RandomAccessFile file=new RandomAccessFile(f,"rw");

			inStream=myFtp.get(textFieldFile.getText());

			while((ch=inStream.read())>=0){

				buf.append((char)ch);

			}

			file.writeBytes(buf.toString());

			file.close();

			JOptionPane.showMessageDialog(FtpClientFrame.this, "下载成功","下载成功!",1);

		}

		catch(Exception e1){

			System.out.println("Error:"+e1);

		}

	}

	

	protected void processWindowEvent(WindowEvent e){

		super.processWindowEvent(e);

		if(e.getID()==WindowEvent.WINDOW_CLOSING){

			try{

				if(inStream!=null)

					inStream.close();

			}

			catch(IOException e1){

				System.out.println("Error: "+e1);

			}

			System.exit(0);

		}

	}

	

	public static void main(String[] args){

		new FtpClientFrame();

	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值