From e86256959bd70bb36ad0f0996cfca94d22dd28e2 Mon Sep 17 00:00:00 2001
From: xuxin <840198532@qq.com>
Date: Thu, 7 May 2026 11:32:45 +0800
Subject: [PATCH] =?UTF-8?q?javaEEday04-Web=E5=85=A5=E9=97=A8-HTTP=E5=8D=8F?=
=?UTF-8?q?=E8=AE=AE-=E6=9C=8D=E5=8A=A1=E7=AB=AF=E5=8D=8F=E8=AE=AE?=
=?UTF-8?q?=E8=A7=A3=E6=9E=90=E6=BC=94=E7=A4=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
http-server-demo/pom.xml | 16 +++
.../src/main/java/com/inmind/Server.java | 98 +++++++++++++++++++
.../src/main/resources/html/a.html | 39 ++++++++
.../src/main/resources/application.properties | 3 +-
.../main/resources/static/01. GET-POST.html | 23 +++++
5 files changed, 178 insertions(+), 1 deletion(-)
create mode 100644 http-server-demo/pom.xml
create mode 100644 http-server-demo/src/main/java/com/inmind/Server.java
create mode 100644 http-server-demo/src/main/resources/html/a.html
create mode 100644 springboot-web-quickstart/src/main/resources/static/01. GET-POST.html
diff --git a/http-server-demo/pom.xml b/http-server-demo/pom.xml
new file mode 100644
index 0000000..96bb233
--- /dev/null
+++ b/http-server-demo/pom.xml
@@ -0,0 +1,16 @@
+
+
+ 4.0.0
+
+ com.itheima
+ http-server-demo
+ 1.0-SNAPSHOT
+
+
+ 11
+ 11
+
+
+
diff --git a/http-server-demo/src/main/java/com/inmind/Server.java b/http-server-demo/src/main/java/com/inmind/Server.java
new file mode 100644
index 0000000..9374d74
--- /dev/null
+++ b/http-server-demo/src/main/java/com/inmind/Server.java
@@ -0,0 +1,98 @@
+package com.inmind;
+
+import java.io.*;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.charset.StandardCharsets;
+
+/*
+ * 自定义web服务器
+ */
+public class Server {
+ public static void main(String[] args) throws IOException {
+ ServerSocket ss = new ServerSocket(8080); // 监听指定端口
+ System.out.println("server is running...");
+
+ while (true){
+ Socket sock = ss.accept();
+ System.out.println("connected from " + sock.getRemoteSocketAddress());
+
+ //开启线程处理请求
+ Thread t = new Handler(sock);
+ t.start();
+ }
+ }
+}
+
+class Handler extends Thread {
+ Socket sock;
+
+ public Handler(Socket sock) {
+ this.sock = sock;
+ }
+
+ public void run() {
+ try (InputStream input = this.sock.getInputStream(); OutputStream output = this.sock.getOutputStream()) {
+ handle(input, output);
+ } catch (Exception e) {
+ try {
+ this.sock.close();
+ } catch (IOException ioe) {
+ }
+ System.out.println("client disconnected.");
+ }
+ }
+
+ /**
+ *
+ * @param input 客户端的请求数据
+ * @param output 可以用来封装客户端的响应数据
+ * @throws IOException
+ */
+ private void handle(InputStream input, OutputStream output) throws IOException {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8));
+
+ // 读取HTTP请求:
+ boolean requestOk = false;
+ String first = reader.readLine();
+ if (first.startsWith("GET / HTTP/1.")) {
+ requestOk = true;
+ }
+
+ for (;;) {
+ String header = reader.readLine();
+ if (header.isEmpty()) { // 读取到空行时, HTTP Header读取完毕
+ break;
+ }
+ System.out.println(header);
+ }
+ System.out.println(requestOk ? "Response OK" : "Response Error");
+
+ if (!requestOk) {// 发送错误响应:
+ writer.write("HTTP/1.0 404 Not Found\r\n");
+ writer.write("Content-Length: 0\r\n");
+ writer.write("\r\n");
+ writer.flush();
+ } else { // 发送成功响应:
+ //读取html文件,转换为字符串
+ InputStream is = Server.class.getClassLoader().getResourceAsStream("html/a.html");
+ BufferedReader br = new BufferedReader(new InputStreamReader(is));
+ StringBuilder data = new StringBuilder();
+ String line = null;
+ while ((line = br.readLine()) != null){
+ data.append(line);
+ }
+ br.close();
+ int length = data.toString().getBytes(StandardCharsets.UTF_8).length;
+
+ writer.write("HTTP/1.1 200 OK\r\n");
+ writer.write("Connection: keep-alive\r\n");
+ writer.write("Content-Type: text/html\r\n");
+ writer.write("Content-Length: " + length + "\r\n");
+ writer.write("\r\n"); // 空行标识Header和Body的分隔
+ writer.write(data.toString());
+ writer.flush();
+ }
+ }
+}
diff --git a/http-server-demo/src/main/resources/html/a.html b/http-server-demo/src/main/resources/html/a.html
new file mode 100644
index 0000000..1e01e3d
--- /dev/null
+++ b/http-server-demo/src/main/resources/html/a.html
@@ -0,0 +1,39 @@
+
+
+
+
+ Title
+
+
+
+
+
+ | 序号 |
+ 品牌名称 |
+ 企业名称 |
+
+
+
+ | 010 |
+ 三只松鼠 |
+ 三只松鼠 |
+
+
+
+ | 009 |
+ 优衣库 |
+ 优衣库 |
+
+
+
+ | 008 |
+ 小米 |
+ 小米科技有限公司 |
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/springboot-web-quickstart/src/main/resources/application.properties b/springboot-web-quickstart/src/main/resources/application.properties
index aa595e2..bb3972c 100644
--- a/springboot-web-quickstart/src/main/resources/application.properties
+++ b/springboot-web-quickstart/src/main/resources/application.properties
@@ -1 +1,2 @@
-spring.application.name=springboot-web-quickstart
\ No newline at end of file
+spring.application.name=springboot-web-quickstart
+server.port=8083
\ No newline at end of file
diff --git a/springboot-web-quickstart/src/main/resources/static/01. GET-POST.html b/springboot-web-quickstart/src/main/resources/static/01. GET-POST.html
new file mode 100644
index 0000000..627b312
--- /dev/null
+++ b/springboot-web-quickstart/src/main/resources/static/01. GET-POST.html
@@ -0,0 +1,23 @@
+
+
+
+
+ 请求方式演示-GET-POST
+
+
+
+
+
+
+
+
+
+