中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java實現簡單的HTTP服務器

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

Java實現簡單的HTTP服務器

Java實現簡單的HTTP服務器

import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.concurrent.*;
import java.io.*;

public 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();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new SimpleHttpServer().service();
    }

}

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());
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            socketChannel.read(buffer);
            buffer.flip();
            String request = decode(buffer);
            StringBuffer sb = new StringBuffer("HTTP/1.1 200 OK\r\n");
            sb.append("Content-Type:text/html\r\n\r\n");
            socketChannel.write(encode(sb.toString()));
            FileInputStream in = null;
            String firstLineOfRequest = request.substring(0,
                    request.indexOf("\r\n"));
            if (firstLineOfRequest.indexOf("login.htm") != -1)
                in = new FileInputStream("login.htm");
            else
                in = new FileInputStream("hello.htm");
            FileChannel fileChannel = in.getChannel();
            fileChannel.transferTo(0, fileChannel.size(), socketChannel);
        } 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);
    }
}

login.htm

<html>
<head>
<title>helloapp</title>
</head>
<form name="loginForm" method="post" action="hello.htm">
<table>
<tr><td><div align="right">用戶名:</div></td>
<td><input type="text" name="username"></td>
</tr>
<tr><td><div align="right">密碼:</div></td>
<td><input type="password" name="password"></td>
</tr>
<tr><td></td>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

標簽: http服務器 服務器

版權申明:本站文章部分自網絡,如有侵權,請聯系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯系。

上一篇:java Velocity 同時初始化兩個 Engine 實例

下一篇:編程算法 - 迷宮的最短路徑 代碼(C++)