Java简单的Web服务器

实现了一个多线程的静态WEB服务器

WebServer

import java.net.* ;  

public final class MultiThreadWebServer {  
public static void main(String argv[]) throws Exception {  

    ServerSocket socket = new ServerSocket(8900);  
    while (true) {  
    // Listen for a TCP connection request.  
    Socket connection = socket.accept();  

    HttpRequest request = new HttpRequest(connection);  

    Thread thread = new Thread(request);  

    thread.start();  
    }  
}  
}  

HttpRequest.java

import java.io.* ;  
import java.net.* ;  
import java.util.* ;  

final class HttpRequest implements Runnable {  

final static String CRLF = "\r\n";  
Socket socket;  

public HttpRequest(Socket socket) throws Exception {  
    this.socket=socket;  
}      

public void run() {  
try {  
        processRequest();  
    } catch (Exception e) {  
        System.out.println(e);  
    }  
}  

private void processRequest() throws Exception {  
    InputStreamReader is=new InputStreamReader(socket.getInputStream());  
    DataOutputStream os=new  DataOutputStream(socket.getOutputStream());  
    BufferedReader br = new BufferedReader(is);  

    String  requestLine;  
    requestLine=br.readLine();  
    System.out.println(requestLine);  

    String headerLine = null;  
    while ((headerLine = br.readLine()).length() != 0) {  
            System.out.println(headerLine);  
    }  

    //Openfile  
    StringTokenizer tokens = new StringTokenizer(requestLine);  
    tokens.nextToken();    
    String fileName = tokens.nextToken();  
    fileName="."+fileName;  

    FileInputStream fis = null ;  
    boolean fileExists = true ;  
    try {  
        fis = new FileInputStream(fileName);  
    } catch (FileNotFoundException e) {  
        fileExists = false ;  
    }  

    // Reponse  
    String statusLine = null;        //状态行  
    String contentTypeLine = null;   //Content-Type行  
    String entityBody = null;        //Entity body部分  
    if (fileExists) {  
        statusLine="HTTP/1.1 200 OK"+CRLF;  
        contentTypeLine = "Content-type: " + contentType( fileName ) + CRLF;  

    } else {  
        statusLine="HTTP/1.1 404 NotFound"+CRLF;  
        contentTypeLine = "Content-type: text/html"+CRLF;  
        entityBody ="<html><title>Not found</title><h1>404 NotFound</h1></html>";  
    }  

    os.writeBytes(statusLine);  
    os.writeBytes(contentTypeLine);  
    os.writeBytes(CRLF);  
    if (fileExists) {  
        sendBytes(fis, os);  
    fis.close();  
    } else {  
        os.writeBytes(entityBody);  
    }  
    System.out.println();  
    os.close();  
    br.close();  
    socket.close();  

}  

private static void sendBytes(FileInputStream fis,OutputStream os) throws Exception {  
     byte[] buffer = new byte[1024];  
     int bytes=0;  
     while((bytes=fis.read(buffer))!=-1){  
         os.write(buffer,0,bytes);  
     }  
}  
private static String contentType(String fileName) {  

    if(fileName.endsWith(".htm") || fileName.endsWith(".html")|| fileName.endsWith(".txt")) {  
           return "text/html";  
    }  
    if(fileName.endsWith(".jpg")) {  
        return "image/jpeg";  
    }  
    if(fileName.endsWith(".gif")) {  
        return "image/gif";  
    }  
    return "application/octet-stream";  

}  

}
‘’’

结果展示
正常显示
文件不存在