java自带的http服务, 虽然无法和tomcat,jetty相比,但是部署方便快捷。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.HttpServer;
public class PageCreaterHttpServer {
public static void main(String[] args) throws IOException {
//创建一个HttpServer实例,并绑定到指定的IP地址和端口号
HttpServer httpServer = HttpServer.create(new InetSocketAddress(8080), 0);
//创建一个HttpContext,将路径为/myserver请求映射到MyHttpHandler处理器
httpServer.createContext("/generate", new TopicPageCreater());
//设置服务器的线程池对象
httpServer.setExecutor(Executors.newFixedThreadPool(10));
//启动服务器
httpServer.start();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
public class TopicPageCreater implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
try {
String requestParam = getRequestParam(httpExchange);
handleResponse(httpExchange);
} catch (Exception ex) {
ex.printStackTrace();
try {
handleFailResponse(httpExchange, ex.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 获取请求头
* @param httpExchange
* @return
*/
private Headers getRequestHeader(HttpExchange httpExchange) {
Headers headers = httpExchange.getRequestHeaders();
return headers;
}
/**
* 获取请求参数
* @param httpExchange
* @return
* @throws Exception
*/
private String getRequestParam(HttpExchange httpExchange) throws Exception {
String paramStr = "";
if (httpExchange.getRequestMethod().toUpperCase().equals("GET")) {
//GET请求读queryString
paramStr = httpExchange.getRequestURI().getQuery();
} else if (httpExchange.getRequestMethod().toUpperCase().equals("POST")) {
//非GET请求读请求体
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody(), "utf-8"));
StringBuilder requestBodyContent = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
requestBodyContent.append(line);
}
paramStr = requestBodyContent.toString();
} else {
throw new RuntimeException("非法请求方式, 请使用post或者get请求");
}
return paramStr;
}
/**
* 处理响应
* @param httpExchange
* @param responsetext
* @throws Exception
*/
private void handleResponse(HttpExchange httpExchange) throws Exception {
//生成html
HashMap<String, String> responseMap = new HashMap<>();
responseMap.put("code", "200");
responseMap.put("msg", "成功");
byte[] responseContentByte = JSON.toJSONString(responseMap).getBytes("utf-8");
//设置响应头,必须在sendResponseHeaders方法之前设置!
httpExchange.getResponseHeaders().add("Content-Type:", "application/json");
//设置响应码和响应体长度,必须在getResponseBody方法之前调用!
httpExchange.sendResponseHeaders(200, responseContentByte.length);
OutputStream out = httpExchange.getResponseBody();
out.write(responseContentByte);
out.flush();
out.close();
}
/**
* 处理响应
* @param httpExchange
* @param responsetext
* @throws Exception
*/
private void handleFailResponse(HttpExchange httpExchange, String msg) throws Exception {
//生成html
HashMap<String, String> responseMap = new HashMap<>();
responseMap.put("code", "500");
responseMap.put("msg", msg);
byte[] responseContentByte = JSON.toJSONString(responseMap).getBytes("utf-8");
//设置响应头,必须在sendResponseHeaders方法之前设置!
httpExchange.getResponseHeaders().add("Content-Type:", "application/json");
//设置响应码和响应体长度,必须在getResponseBody方法之前调用!
httpExchange.sendResponseHeaders(200, responseContentByte.length);
OutputStream out = httpExchange.getResponseBody();
out.write(responseContentByte);
out.flush();
out.close();
}
}
这边可能HttpServer找不到, 是因为eclipse 对com.sun.net.httpserver.HttpServer 没有访问权限