• 简单的java http服务器


    package testreadline;
    import java.net.*;
    import java.nio.ByteBuffer;
    import java.nio.channels.*;
    import java.nio.charset.*;
    import java.util.concurrent.*;
    import java.io.*;
    public class test {
    
     public static void main(String[] args) throws IOException {
      new SimpleHttpServer().service();
      
     }
    }
     class SimpleHttpServer {
     private int port = 80;
     private ServerSocketChannel serverSocketChannel = null;
     private ExecutorService executorService;
     private static final int POOL_MULTIPLE = 4;
    
     public SimpleHttpServer() throws IOException 
     {
      executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
        .availableProcessors() * POOL_MULTIPLE);
      serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.socket().setReuseAddress(true);
      serverSocketChannel.socket().bind(new InetSocketAddress(port));
     }
    
     public void service() {
      while (true) {
       SocketChannel socketChannel = null;
       try {
        socketChannel = serverSocketChannel.accept();
        executorService.execute(new Handler(socketChannel));
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
     }
    
    
    }
    
    class Handler implements Runnable {
     private SocketChannel socketChannel;
     public Handler(SocketChannel socketChannel) {
      this.socketChannel = socketChannel;
     }
    
     @Override
     public void run() {
      handle(socketChannel);
     }
    
     private void handle(SocketChannel socketChannel) {
      try {
       Socket socket = socketChannel.socket();  
       System.out.println(socket.getInetAddress() + ":" + socket.getPort());
          String info = "HTTP/1.1 200 OK
    " +
                           "Server: Apache-Coyote/1.1
    " +
                           "Content-Type: text/html;charset=utf-8
    " +
                           "Content-Length: 1021
    " +
                           "Date: Wed, 09 Dec 2009 05:00:27 GMT
    " +
                           "
    ";                
                   OutputStream os = socket.getOutputStream();
                   os.write(info.getBytes("utf-8"));
                   os.flush();
                   String html="<H1>港港都是泪,还是早停困!</H1>";
                   os.write(html.getBytes("utf-8"));
                   os.flush();
      
      
      
      } catch (IOException e) {
       e.printStackTrace();
      } finally {
       try {
        if (socketChannel != null)
         socketChannel.close();
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
     }
    
     private Charset charset = Charset.forName("GBK");
    
     private ByteBuffer encode(String string) {
      return ByteBuffer.allocate(string.length() * 2).get(
        string.getBytes(charset));
     }
    
     private String decode(ByteBuffer buffer) {
      byte[] source = new byte[buffer.position() + 1];
      buffer.put(source);
      return new String(source, charset);
     }
    }
    
  • 相关阅读:
    c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
    无废话MVC入门教程二[第一个小Demo]
    winform使用xml作为数据源
    SQL Server 2005为数据库设置用户名和密码的方法
    数据库 的版本为 661,无法打开。此服务器支持 655 版及更低版本。不支持降级路径。
    Visual Studio快速封装字段方法
    erp crm oa
    sqldbhelper
    OleDbHelper
    存储过程分页(3)
  • 原文地址:https://www.cnblogs.com/egai/p/3599527.html
Copyright © 2020-2023  润新知