九游会注册

让建站和SEO变得简单

让不懂建站的用户快速建站,让会建站的提高建站效率!

你的位置:九游会注册_九游会j9登录官网 > 九游会j9产品中心 > 九游会注册 [NIO]NIO已矣HTTP职业器

九游会注册 [NIO]NIO已矣HTTP职业器

时间:2022-06-10 10:42 点击:102 次

NIO已矣的HTTP职业器

本篇著述的代码防护相比少,详备的NIO的责任经由可参考[NIO]NIO版块的鸿儒聊天室

/***NIO已矣HTTP职业器**@authorfutao*@date2020/7/10*/@Slf4jpublicclassNioHttpServer{privatestaticfinalByteBufferREAD_BUFFER=ByteBuffer.allocate(1024*4);/***静态资源旅途*/privatestaticfinalStringSTATIC_RESOURCE_PATH=System.getProperty("user.dir")+"/practice/src/main/resources/pages/";/***反馈的基础信息*/publicstaticfinalStringBASIC_RESPONSE="HTTP/1.1200OK\r\n"+"Content-Type:text/html;charset=utf-8\r\n"+"Vary:Accept-Encoding\r\n";/***回车换行符*/privatestaticfinalStringcarriageReturn="\r\n";publicvoidstart{try{ServerSocketChannelserverSocketChannel=ServerSocketChannel.open;serverSocketChannel.configureBlocking(false);serverSocketChannel.bind(newInetSocketAddress("localhost",Constants.SERVER_PORT));Selectorselector=Selector.open;serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);while(true){inteventCountTriggered=selector.select;if(eventCountTriggered==0){continue;}SetselectionKeys=selector.selectedKeys;for(SelectionKeyselectionKey:selectionKeys){handleSelectKey(selectionKey,selector);}selectionKeys.clear;}}catch(IOExceptione){e.printStackTrace;}}publicvoidhandleSelectKey(SelectionKeyselectionKey,Selectorselector){if(selectionKey.isAcceptable){ServerSocketChannelserverSocketChannel=(ServerSocketChannel)selectionKey.channel;try{SocketChannelsocketChannel=serverSocketChannel.accept;socketChannel.configureBlocking(false);socketChannel.register(selector,SelectionKey.OP_READ);log.debug("客户端[{}]接入",socketChannel.socket.getPort);}catch(IOExceptione){e.printStackTrace;}}elseif(selectionKey.isReadable){READ_BUFFER.clear;SocketChannelsocketChannel=(SocketChannel)selectionKey.channel;try{while(socketChannel.read(READ_BUFFER)>0){}READ_BUFFER.flip;StringrequestMessage=String.valueOf(Constants.CHARSET.decode(READ_BUFFER));log.info("罗致到浏览器发来的数据:\n{}===requestprintend...",requestMessage);if(StringUtils.isBlank(requestMessage)){selectionKey.cancel;selector.wakeup;}StringrequestUri=NioHttpServer.getRequestUri(requestMessage);staticHandler(requestUri,socketChannel);selectionKey.cancel;selector.wakeup;}catch(IOExceptione){e.printStackTrace;}}}/***取得苦求的资源地址**@paramrequest*@return*/privatestaticStringgetRequestUri(Stringrequest){//GET/index.htmlHTTP/1.1intfirstBlank=request.indexOf("");StringexcludeMethod=request.substring(firstBlank+2);returnexcludeMethod.substring(0,excludeMethod.indexOf(""));}/***静态资源处分器**@return*/publicbooleanstaticHandler(Stringpage,SocketChannelsocketChannel)throwsIOException{//资源的完全旅途StringfilePath=NioHttpServer.STATIC_RESOURCE_PATH+page;booleanfileExist=false;Filefile=newFile(filePath);if(file.exists&&file.isFile){log.debug("静态资源[{}]存在",page);fileExist=true;//读取文献现实byte[]bytes=Files.readAllBytes(Paths.get(filePath));ByteBufferbuffer=ByteBuffer.allocate(4*1024);buffer.put(BASIC_RESPONSE.getBytes(Constants.CHARSET));buffer.put(("Server:futaoServerBaseNIO/1.1"+NioHttpServer.carriageReturn).getBytes(Constants.CHARSET));buffer.put(("content-length:"+bytes.length+NioHttpServer.carriageReturn).getBytes(Constants.CHARSET));buffer.put(NioHttpServer.carriageReturn.getBytes(Constants.CHARSET));buffer.put(bytes);buffer.flip;while(buffer.hasRemaining){socketChannel.write(buffer);}}returnfileExist;}publicstaticvoidmain(String[]args){newNioHttpServer.start;}}

测试

#源代码九游会注册

九游会j9服务热线
官方网站:http://www.guesstheword.net/
工作时间:周一至周六(09:00-18:00)
联系我们
QQ:2852320325
邮箱:w365jzcom@qq.com
地址:武汉东湖新技术开发区光谷大道国际企业中心
关注公众号

Powered by 九游会注册_九游会j9登录官网 RSS地图 HTML地图

九游会j9
九游会注册_九游会j9登录官网-九游会注册 [NIO]NIO已矣HTTP职业器

回到顶部