怎么查找网站死链,安卓代理ip软件,怎么做网站底部版权信息,包装设计效果图生成器2024年10月14日14:17:07—0.6.0 java-实现一个简单的httpserver-0.6.0 背景功能具体代码打印 背景
通常写了一些接口#xff0c;需要通过临时的http访问#xff0c;又不需要spring这么厚重的框架
功能
设置并发监控并发两个get请求一个是根路径#xff0c;一个是other增加… 2024年10月14日14:17:07—0.6.0 java-实现一个简单的httpserver-0.6.0 背景功能具体代码打印 背景
通常写了一些接口需要通过临时的http访问又不需要spring这么厚重的框架
功能
设置并发监控并发两个get请求一个是根路径一个是other增加了一个post请求的方法other1
具体代码
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;public class server {private static AtomicInteger concurrentConnections new AtomicInteger(0);public static void main(String[] args) throws IOException {HttpServer server HttpServer.create(new InetSocketAddress(localhost, 8222), 0);// 处理根路径get请求server.createContext(/, new RootHandler());// 处理 /other 路径get请求server.createContext(/other, new OtherHandler());// 处理 /other1 路径post请求server.createContext(/other1, new Other1Handler());// 设置并发连接数server.setExecutor(java.util.concurrent.Executors.newFixedThreadPool(10));server.start();System.out.println(Server started on port 8000.);// 定期打印当前并发连接数new Thread(() - {while (true) {try {Thread.sleep(5000);System.out.println(Current concurrent connections: concurrentConnections.get());} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}).start();}static class RootHandler implements HttpHandler {Overridepublic void handle(HttpExchange exchange) throws IOException {concurrentConnections.incrementAndGet();if (GET.equals(exchange.getRequestMethod())) {String response Hello from root path!;exchange.sendResponseHeaders(200, response.length());OutputStream os exchange.getResponseBody();os.write(response.getBytes());os.close();// 监控连接释放事件System.out.println(root Connection released);}concurrentConnections.decrementAndGet();}}static class OtherHandler implements HttpHandler {Overridepublic void handle(HttpExchange exchange) throws IOException {concurrentConnections.incrementAndGet();if (GET.equals(exchange.getRequestMethod())) {String response This is a response for /other path.;exchange.sendResponseHeaders(200, response.length());OutputStream os exchange.getResponseBody();os.write(response.getBytes());os.close();}// 监控连接释放事件System.out.println(/other Connection released);concurrentConnections.decrementAndGet();}}static class Other1Handler implements HttpHandler {Overridepublic void handle(HttpExchange exchange) throws IOException {concurrentConnections.incrementAndGet();if (POST.equals(exchange.getRequestMethod())) {String response This is a response for /other1 post path.;exchange.sendResponseHeaders(200, response.length());OutputStream os exchange.getResponseBody();os.write(response.getBytes());os.close();}// 监控连接释放事件System.out.println(/other Connection released);concurrentConnections.decrementAndGet();}}
}
打印
root Connection released
Current concurrent connections: 0
/other Connection released
root Connection released
/other Connection released
/other Connection released
/other Connection released
/other Connection released
/other Connection released
Current concurrent connections: 0
/other Connection released
/other Connection released
/other Connection released
root Connection released
Current concurrent connections: 0
Current concurrent connections: 0
文章转载自: http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.ktdqu.cn.gov.cn.ktdqu.cn http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.mwjwy.cn.gov.cn.mwjwy.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.kqxng.cn.gov.cn.kqxng.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.zlchy.cn.gov.cn.zlchy.cn http://www.morning.vehna.com.gov.cn.vehna.com http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.txnqh.cn.gov.cn.txnqh.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.gxhqt.cn.gov.cn.gxhqt.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.tfbpz.cn.gov.cn.tfbpz.cn http://www.morning.msgnx.cn.gov.cn.msgnx.cn http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.lgkbn.cn.gov.cn.lgkbn.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.gwdmj.cn.gov.cn.gwdmj.cn http://www.morning.pfggj.cn.gov.cn.pfggj.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.kbyp.cn.gov.cn.kbyp.cn http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.knswz.cn.gov.cn.knswz.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.cmldr.cn.gov.cn.cmldr.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn