JAVA实现简单的FTP服务器

JAVA实现简单的ftp服务器

通过界面可启动FTP服务,先看看看效果,然后具体代码附后。

点击启动FTP服务后,可以看到控制台: 

可以通过get和send以及dir执行相对应的操作。

点击退出按钮,关闭服务。

具体代码

JAVA版本如下:

整个项目如下:

 需要用到的jar包下载

下载链接:https://dom4j.github.io/

下载链接:https://blog.csdn.net/a520songhai/article/details/80877076

下载链接:https://pan.baidu.com/s/1i35kvWT

Start_ftp.java

package ftpServer;

import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import ftpServer.FtpServer;
  
public class Start_ftp extends JFrame implements ActionListener {
 
	public static String randomcaptcha;
	public JLabel logoLabel, userNameLabel, passwordLabel, captchaLabel;
	public JButton login, logout;
	public Panel panel;
  
 public Start_ftp() {
	 setTitle("FTP服务器");
	 setSize(400, 300);
	 setLocationRelativeTo(null);
	 init();
	 setVisible(true);
	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 setResizable(false);
 }
  
 public void init() {
	 setLayout(null);
	 // logoLabel= new JLabel();
	 // logoLabel.setIcon(new ImageIcon("E:\\eclipse\\student3\\welcome.gif"));
	 logoLabel = new JLabel(new ImageIcon("image/aa.gif"));
	 logoLabel.setBounds(125, 10, 150, 70);
	 add(logoLabel);
  
	 login = new JButton("启动FTP");
	 login.setBounds(70, 180, 120, 30);
	 login.setMnemonic(KeyEvent.VK_L);
	 add(login);
	 logout = new JButton("退出");
	 logout.setBounds(210, 180, 120, 30);
	 logout.setMnemonic(KeyEvent.VK_X);
	 add(logout);
  
	 login.addActionListener(this);
	 logout.addActionListener(this);
 
 }
  
 public void actionPerformed(ActionEvent e) {
  
	 if (e.getSource() == login) {
	  
	 try {
		FtpServer ftpServer = new FtpServer(21);
		ftpServer.listen();
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}	  
  }
  if (e.getSource() == logout) {
	  JOptionPane.showMessageDialog(this, "退出");	  
	  new QuitCommand();
	  System.exit(0);
	  //dispose();
  	}
 }
 
 public static void main(String[] args) {
	 new Start_ftp();
 	}
}

Command.java

package ftpServer;
 
import java.io.Writer;
 
interface Command {
 
	/**
	 * @param data    从ftp客户端接收的除ftp命令之外的数据
	 * @param writer  网络输出流
	 * @param t       控制连接所对应的处理线程
	 * */
	public void getResult(String data,Writer writer,ControllerThread t);
	
}

CommandFactory.java

package ftpServer;
 
public class CommandFactory {
 
	public static Command createCommand(String type) {
		
		type = type.toUpperCase();
		switch(type)
		{
			case "USER":return new UserCommand();
			
			case "PASS":return new PassCommand();
			
			case "LIST":return new DirCommand();
			
			case "PORT":return new PortCommand();
			
			case "QUIT":return new QuitCommand();
			
			case "RETR":return new RetrCommand();
			
			case "CWD":return new CwdCommand();
			
			case "STOR":return new StoreCommand();
			
			default :return null;
		}
		
	}
}

 ControllerThread.java

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蹦跶的小羊羔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值