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>
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10465.html