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

📄 httpserver.java

📁 实现Web服务器和浏览器功能
💻 JAVA
字号:
package WebServer;import java.net.Socket;import java.net.ServerSocket;import java.net.InetAddress;import java.io.InputStream;import java.io.OutputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.File;import java.awt.event.*;import java.net.*;public class HttpServer extends java.awt.Frame {	private int iPort = 80;	//default port	public static String WEB_ROOT = System.getProperty("user.dir") + File.separator  + "webroot";	//default web root	public HttpServer()	{		addWindowListener(new WindowAdapter() {			public void windowClosing(WindowEvent e) {				dispose();				System.exit(0);			}		});		getConfig();		this.start();	}	public static void main(String[] args)  {    	HttpServer server = new HttpServer();	}	private void getConfig() {	//get web root directory and port from file "config.ini"		int BUFFER_SIZE = 1024;	    byte[] bytes = new byte[BUFFER_SIZE];	    FileInputStream fis = null;	    try {	      File file = new File("config.ini");	      if (file.exists()) {	        fis = new FileInputStream(file);	        int ch = fis.read(bytes, 0, BUFFER_SIZE);	        if (ch!=-1) {	        	String config = new String(bytes);				int webRootIndex = config.indexOf("webroot: ");				if (webRootIndex != -1) 					WEB_ROOT = config.substring(webRootIndex + 9, config.indexOf('\n',webRootIndex)).trim();				if (WEB_ROOT.equals("")) WEB_ROOT = "webroot";	//default web root				WEB_ROOT = System.getProperty("user.dir") + File.separator  + WEB_ROOT;				System.out.println("web root:   " + WEB_ROOT);									int portIndex = config.indexOf("port: ");				if (portIndex != -1) 					iPort = Integer.parseInt(config.substring(portIndex + 6, config.indexOf('\n',portIndex)).trim());				if (iPort == 0) iPort = 80;	//default port				System.out.println("prot:       " + String.valueOf(iPort));				System.out.println("");	        }	      }	      else {	        // file not found	        System.out.println("config file not found.");	      }	    }	    catch (Exception e) {	      // thrown if cannot instantiate a File object	      System.out.println(e.toString() );	    }	    finally {	      if (fis!=null)	      	try{	        	fis.close();	     	}	     	catch (Exception e){}	    }	}	public void start() {		System.out.println("web sever starting...");		ServerSocket serverSocket = null;    	try {			serverSocket =  new ServerSocket(iPort, 1);    	}    	catch (IOException e) {      		e.printStackTrace();      		System.exit(1);    	}		System.out.println("web sever started");		System.out.println("");		    // Loop waiting for a request		while (true) {			Socket socket = null;			InputStream input = null;			OutputStream output = null;			try {				socket = serverSocket.accept();				input = socket.getInputStream();				output = socket.getOutputStream();				// create Request object and parse				Request request = new Request(input);				request.parse();				// create Response object				Response response = new Response(output);        		response.setRequest(request);        		response.sendStaticResource();        		// Close the socket        		socket.close();      		}      		catch (Exception e) {        		e.printStackTrace();        		continue;      		}    	}  	}}

⌨️ 快捷键说明

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