加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
01factory.java 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
daokes 提交于 2021-04-22 14:25 . add pages
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class HttpServer01factory {
public static void main(String[] args) throws IOException {
InetSocketAddress addr = new InetSocketAddress(8888);
HttpServer server = HttpServer.create(addr, 0);
server.createContext("/01factory", new Handler());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on port 8888" );
}
}
class Handler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String requestMethod = exchange.getRequestMethod();
if (requestMethod.equalsIgnoreCase("GET")) {
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Access-Control-Allow-Origin", "*");
responseHeaders.set("Access-Control-Allow-Credentials", "true");
responseHeaders.set("Access-Control-Allow-Methods", "GET");
responseHeaders.set("Content-Type", "application/json; chatset=utf-8");
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
String ret = "{\"01factory\":\"零壹微工厂\"}";
responseBody.write(ret.getBytes());
responseBody.close();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化