zoukankan      html  css  js  c++  java
  • 简单的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);
     }
    }
    
  • 相关阅读:
    Eagle+微力同步实现素材资源协同共享
    Eagle+欧奥PicHome创建私有的pinterest网站
    C# 从Json中获取byte[] 二进制数据
    Vivado SDK 2016.4改JTAG速度
    启动jmeter,提示:unable to access jarfile apacheJmeter.jar
    直播来啦!亮相微软平台,聊聊Dapr的落地应用
    互斥锁、自旋锁、读写锁...理清它们的区别和应用
    行云创新:云原生技术助力企业数字化转型
    行云创新亮相“OSCAR开源产业大会”:云调试加速应用创新
    Actor模型是如何让编写并发系统变得更简单的?
  • 原文地址:https://www.cnblogs.com/egai/p/3599527.html
Copyright © 2011-2022 走看看