Runtime.getRuntime().availableProcessors() * POOL_MULTIPLE);
serverSocketChannel= ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
System.out.println("服务器启动");
}
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{ //Handler是内部类,负责处理HTTP请求
private SocketChannel socketChannel;
public Handler(SocketChannel socketChannel){
this.socketChannel=socketChannel;
}
public void run(){
handle(socketChannel);
}
public void handle(SocketChannel socketChannel){
try {
Socket socket=socketChannel.socket();
System.out.println("接收到客户连接,来自: " +
socket.getInetAddress() + ":" +socket.getPort());
ByteBuffer buffer=ByteBuffer.allocate(1024);
socketChannel.read(buffer); //接收HTTP请求,假定其长度不超过1024个字节
buffer.flip();
String request=decode(buffer);
System.out.print(request); //打印HTTP请求
//生成HTTP响应结果
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())); //发送HTTP响应的第一行和响应头
FileInputStream in;
//获得HTTP请求的第一行
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 (Exception e) {
e.printStackTrace();
}finally {
try{
if(socketChannel!=null)socketChannel.close(); //关闭连结
}catch (IOException e) {e.printStackTrace();}
}
}
|