JAVA实现简单的ftp服务器
通过界面可启动FTP服务,先看看看效果,然后具体代码附后。
点击启动FTP服务后,可以看到控制台:
可以通过get和send以及dir执行相对应的操作。
点击退出按钮,关闭服务。
具体代码
JAVA版本如下:
整个项目如下:
需要用到的jar包下载
下载链接: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;
}
}
}