Java实现简单的HTTP服务器代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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);
    }
}