[Java] Java实现简单的HTTP服务器 →→→→→进入此内容的聊天室

来自 , 2019-09-12, 写在 Java, 查看 139 次.
URL http://www.code666.cn/view/b85d65c3
  1. import java.net.*;
  2. import java.nio.ByteBuffer;
  3. import java.nio.channels.*;
  4. import java.nio.charset.*;
  5. import java.util.concurrent.*;
  6. import java.io.*;
  7.  
  8. public class SimpleHttpServer {
  9.         private int port = 80;
  10.         private ServerSocketChannel serverSocketChannel = null;
  11.         private ExecutorService executorService;
  12.         private static final int POOL_MULTIPLE = 4;
  13.  
  14.         public SimpleHttpServer() throws IOException {
  15.                 executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
  16.                                 .availableProcessors() * POOL_MULTIPLE);
  17.                 serverSocketChannel = ServerSocketChannel.open();
  18.                 serverSocketChannel.socket().setReuseAddress(true);
  19.                 serverSocketChannel.socket().bind(new InetSocketAddress(port));
  20.         }
  21.  
  22.         public void service() {
  23.                 while (true) {
  24.                         SocketChannel socketChannel = null;
  25.                         try {
  26.                                 socketChannel = serverSocketChannel.accept();
  27.                                 executorService.execute(new Handler(socketChannel));
  28.                         } catch (IOException e) {
  29.                                 e.printStackTrace();
  30.                         }
  31.                 }
  32.         }
  33.  
  34.         public static void main(String[] args) throws IOException {
  35.                 new SimpleHttpServer().service();
  36.         }
  37.  
  38. }
  39.  
  40. class Handler implements Runnable {
  41.         private SocketChannel socketChannel;
  42.         public Handler(SocketChannel socketChannel) {
  43.                 this.socketChannel = socketChannel;
  44.         }
  45.  
  46.         @Override
  47.         public void run() {
  48.                 handle(socketChannel);
  49.         }
  50.  
  51.         private void handle(SocketChannel socketChannel) {
  52.                 try {
  53.                         Socket socket = socketChannel.socket();
  54.                         System.out
  55.                                         .println(socket.getInetAddress() + ":" + socket.getPort());
  56.                         ByteBuffer buffer = ByteBuffer.allocate(1024);
  57.                         socketChannel.read(buffer);
  58.                         buffer.flip();
  59.                         String request = decode(buffer);
  60.                         StringBuffer sb = new StringBuffer("HTTP/1.1 200 OK\r\n");
  61.                         sb.append("Content-Type:text/html\r\n\r\n");
  62.                         socketChannel.write(encode(sb.toString()));
  63.                         FileInputStream in = null;
  64.                         String firstLineOfRequest = request.substring(0,
  65.                                         request.indexOf("\r\n"));
  66.                         if (firstLineOfRequest.indexOf("login.htm") != -1)
  67.                                 in = new FileInputStream("login.htm");
  68.                         else
  69.                                 in = new FileInputStream("hello.htm");
  70.                         FileChannel fileChannel = in.getChannel();
  71.                         fileChannel.transferTo(0, fileChannel.size(), socketChannel);
  72.                 } catch (IOException e) {
  73.                         e.printStackTrace();
  74.                 } finally {
  75.                         try {
  76.                                 if (socketChannel != null)
  77.                                         socketChannel.close();
  78.                         } catch (IOException e) {
  79.                                 e.printStackTrace();
  80.                         }
  81.                 }
  82.         }
  83.  
  84.         private Charset charset = Charset.forName("GBK");
  85.  
  86.         private ByteBuffer encode(String string) {
  87.                 return ByteBuffer.allocate(string.length() * 2).get(
  88.                                 string.getBytes(charset));
  89.         }
  90.  
  91.         private String decode(ByteBuffer buffer) {
  92.                 byte[] source = new byte[buffer.position() + 1];
  93.                 buffer.put(source);
  94.                 return new String(source, charset);
  95.         }
  96. }
  97.  
  98. //java/1308

回复 "Java实现简单的HTTP服务器"

这儿你可以回复上面这条便签

captcha