java用socket来做http服务器


import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
  
public class HttpServer { 
  private static final Integer port = 80;//HTTP默认端口80 
  
  public static void main(String[] args) { 
    ServerSocket serverSocket; 
  
    try { 
      //建立服务器Socket,监听客户端请求 
      serverSocket = new ServerSocket(port); 
      System.out.println("服务器端口:"+serverSocket.getLocalPort()); 
      //死循环不间断监听客户端请求 
      while(true){ 
        final Socket socket = serverSocket.accept(); 
        System.out.println("接收到一个请求,请求地址:"+  
            socket.getInetAddress()+":"+socket.getPort()); 
        //并发处理HTTP客户端请求 
        service(socket); 
      } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
  
  public static void service(final Socket socket) 
  { 
    new Thread(){ 
      public void run(){ 
        InputStream inSocket; 
        try { 
          //获取HTTP请求头 
          inSocket = socket.getInputStream(); 
          int size = inSocket.available(); 
          byte[] buffer = new byte[size]; 
          inSocket.read(buffer); 
          String request = new String(buffer); 
          System.out.println("ClientBrowser:\n"+request+"\n"
              + "------------------------------------------------------------------"); 
  
          String firstLineOfRequest = ""; //http报文头
          String[] heads; //报文头的数据集合
          String uri = "/index.html"; 
          String contentType =""; 
          if(request.length() > 0){ 
            firstLineOfRequest = request.substring(0,request.indexOf("\r\n")); 
            heads = firstLineOfRequest.split(" "); 
            uri = heads[1]; 
  
            if(uri.indexOf("html") != -1){ 
              contentType = "text/html"; 
            }else{ 
              contentType = "application/octet-stream"; 
            } 
          } 
          System.out.println("http报文内容:"+request.substring(request.indexOf("\r\n"),request.length()));
          
          //将响应头发送给客户端 
          String responseFirstLine = "HTTP/1.1 200 OK\r\n"; 
  
          String responseHead = "Content-Type:" + contentType +"\r\n"; 
  
          OutputStream outSocket = socket.getOutputStream(); 
          System.out.println("ServerResponse:\n"+responseFirstLine+"\n"+responseHead+"\n"
              + "--------------------------------------------------------------------"); 
          outSocket.write(responseFirstLine.getBytes()); 
          outSocket.write(responseHead.getBytes()); 
          //通过HTTP请求中的uri读取相应文件发送给客户端 
          FileInputStream writehtml = new FileInputStream(new File("wwwroot"+uri)); 
          outSocket.write("\r\n".getBytes());  
          byte[] htmlbuffer = new byte[writehtml.available()]; 
          if(writehtml !=null){ 
            int len = 0; 
            System.out.println("writeHtml"); 
            while((len = writehtml.read(htmlbuffer)) != -1){ 
              outSocket.write(htmlbuffer, 0,len); 
            } 
          } 
          outSocket.close(); 
          socket.close(); 
  
        } catch (IOException e) { 
          e.printStackTrace(); 
        } 
      } 
    }.start(); 
  } 
  
}

 

你可能感兴趣的:(java-util)