Java实现简单的HTTP服务器代码
发布时间:2020-12-14 23:53:43 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.net.*;import java.nio.ByteBuffer;import java.nio.channels.*;import java.nio.charset.*;import java.util.concurrent.*;import java.
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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 OKrn"); sb.append("Content-Type:text/htmlrnrn"); socketChannel.write(encode(sb.toString())); FileInputStream in = null; String firstLineOfRequest = request.substring(0,request.indexOf("rn")); 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); } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |