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

📄 webserver.java

📁 Java版Web Sever
💻 JAVA
字号:
import java.awt.event.* ;
import java.awt.*;
import javax.swing.*;

public final class WebServer implements ActionListener
{
	public JFrame f = new JFrame("Web服务器");
	public JPanel p1 = new JPanel();
	public JPanel p2 = new JPanel();
	public JPanel p3 = new JPanel();
	public JPanel p4 = new JPanel();
	public JPanel p5 = new JPanel();
	public JPanel pc = new JPanel();
	public JPanel pe = new JPanel();
	public JPanel pn = new JPanel();
	public JPanel ps = new JPanel();
	public JPanel pw = new JPanel();
	public JLabel lip = new JLabel("IP地址");
	public JLabel lpath = new JLabel("目录 ");
	public JLabel lport = new JLabel("端口");
	public JButton open = new JButton("打开");
	public JButton bstart = new JButton("启动");
	public JButton bstop = new JButton("停止");
	public JButton bclean = new JButton("清空");
	public JButton bquit = new JButton("退出");
	public JTextField tip = new JTextField("",18);
	public JTextField tpath = new JTextField("C:\\",20);
	public JTextField tport = new JTextField("8080",6);
	public JTextArea message = new JTextArea("",7,10);
	public JScrollPane jsp = new JScrollPane(message);
	public JFileChooser chose = new JFileChooser();
	ServerThread listening; //监听线程
	boolean hasStarted = false; //启动状态
	
	WebServer()
	{
		f.setLayout(new BorderLayout());
		pc.setLayout(new BorderLayout());
    	p1.setLayout(new BorderLayout());
   		p2.setLayout(new FlowLayout());
   		p3.setLayout(new BorderLayout());
   		p4.setLayout(new GridLayout(1,4,10,10));
   		p5.setLayout(new BorderLayout());

		bstart.setForeground(Color.red);
    	bstop.setForeground(Color.red);
		bstop.setEnabled(false);
    	bclean.setForeground(Color.red);
    	bquit.setForeground(Color.red);
    	lip.setForeground(Color.blue);
    	lpath.setForeground(Color.blue);
    	lport.setForeground(Color.blue);
   		open.setForeground(Color.blue);
   		chose.setFileSelectionMode(1);
   		message.setForeground(Color.magenta);
   		try{
   			String s = java.net.InetAddress.getLocalHost().toString();
   			int i = s.indexOf("/");
   			s = s.substring(++i);
   			tip.setText(s);
   		}catch(Exception e){
   			tip.setText("127.0.0.1");
   		}
   		
		p2.add(lip);
		p2.add(tip);
		p2.add(lport);
		p2.add(tport);
		p3.add(lpath,"West");
		p3.add(tpath,"Center");
		p3.add(open,"East");
    	p4.add(bstart);
    	p4.add(bstop);
    	p4.add(bclean);
    	p4.add(bquit);   
    	p5.add(jsp);	 	
    	p1.add(p2,"North");
    	p1.add(p3,"Center");
    	p1.add(p4,"South");    	
    	pc.add(p1,"North");
    	pc.add(p5,"South");
    	
    	f.getContentPane().add(pc,"Center");
    	f.getContentPane().add(pn,"North");
    	f.getContentPane().add(pe,"East");
    	f.getContentPane().add(pw,"West");
    	f.getContentPane().add(ps,"South");
    	f.setSize(420,272);
    	f.setLocation(100,100);
    	f.setVisible(true);
    	f.setResizable(false);

    	open.addActionListener(this);
  		bstart.addActionListener(this);
  		bstop.addActionListener(this);
  		bclean.addActionListener(this);
 		bquit.addActionListener(this);
	}
	
	public void opens()
	{
		if (chose.showOpenDialog(f) == JFileChooser.APPROVE_OPTION)
			tpath.setText("" + chose.getSelectedFile().getAbsolutePath());
	}
	
	public void Starts(){
		String path;
		bstart.setEnabled(false);	
		bstop.setEnabled(true);
		message.append("已启动监听\n\n");
		if(hasStarted){
            listening.resume(); //恢复线程
		}
		else{
            hasStarted = true;
            int port = 8080; //默认端口
            try{
				port = Integer.parseInt(tport.getText()); //设置端口号
            }
			catch (NumberFormatException e){
				message.append("端口号无效,自动切换到端口8080\n");
			}
			path=tpath.getText();
            listening = new ServerThread(port, message,path); //等待新的连接
            listening.start(); //启动线程
		}
	}
	
	public void Stops()
	{
		bstart.setEnabled(true);
		bstop.setEnabled(false);
		listening.suspend(); //暂停线程
		message.append("已停止监听\n");
	}
	
	public void Quits()
	{
		if(listening !=null)	listening.stop(); //终止线程
		System.exit(0);
	}
	
	public void actionPerformed (ActionEvent event) //事件响应
	{
		String command = event.getActionCommand();
		if(command.equals("打开"))	opens();
		if(command.equals("启动"))	Starts();
		if(command.equals("停止"))	Stops();
		if(command.equals("清空"))	message.setText("");
		if(command.equals("退出"))	Quits();
	}
	
	public static void main(String args[]) throws Exception
	{
		new WebServer();
	}
}

⌨️ 快捷键说明

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