master
yaoxin 2024-04-17 11:15:15 +08:00
commit 15f14c758c
12 changed files with 852 additions and 0 deletions

33
.gitignore vendored 100644
View File

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

86
pom.xml 100644
View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bwie</groupId>
<artifactId>lx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lx</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Alibaba Fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.80</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.bwie.lx.LxApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,321 @@
package com.bwie.lx;
import com.alibaba.fastjson.JSON;
import com.bwie.lx.doamin.BookInfo;
import java.io.*;
import java.lang.reflect.Field;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName ClientHandler
* @Description
* @Author Xin.Yao
* @Date 2024/4/15 13:46
*/
class ClientHandler implements Runnable {
private Socket clientSocket;
private String fileName="D:\\work\\lx\\src\\main\\java\\com\\bwie\\lx\\txt\\BookInfo.txt";
public ClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
// 判断对象中是否存在指定名称的属性
public static boolean hasField(Object obj, String fieldName) {
Class<?> clazz = obj.getClass();
try {
// 获取指定名称的属性getField() 方法只能获取公共的属性
Field field = clazz.getDeclaredField(fieldName);
// 如果获取成功,说明属性存在
return field != null;
} catch (NoSuchFieldException e) {
// 如果抛出 NoSuchFieldException 异常,说明属性不存在
return false;
}
}
public List<BookInfo> getBookList(){
List<BookInfo> bookList = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
String line;
List<String> list = new ArrayList<>();
while ((line = reader.readLine()) != null) {
list.add(line);
}
list.remove(0);
bookList = list.stream()
.map(book -> {
return new BookInfo().bookInfoBuild(book.split("\\|"));
}).collect(Collectors.toList());
System.out.println(bookList);
}catch (IOException e){
return null;
}
return bookList;
}
//对所查询的字段进行拼接
public String spliceBookInfo(List<BookInfo> bookInfoList,String statsStr){
String str="";
try {
if (statsStr.equals("*")){
BufferedReader reader = new BufferedReader(new FileReader(fileName));
for (int i = 0; i < 1; i++) {
statsStr=reader.readLine();
}
if (bookInfoList!=null){
for (BookInfo bookInfo : bookInfoList) {
str=str+";"+bookInfo.getString();
}
str=str.substring(1);
}
}else{
String[] split = statsStr.split(",");
String head="";
for (String s : split) {
head=head+"|"+s;
}
if (bookInfoList!=null){
for (BookInfo bookInfo : bookInfoList) {
Class<? extends BookInfo> aClass = bookInfo.getClass();
String fild="";
for (String s1 : split) {
Field field = aClass.getDeclaredField(s1);
field.setAccessible(true);
Object o = field.get(bookInfo);
fild=fild+"|"+o;
}
fild=fild.substring(1);
str=str+";"+fild;
}
str=str.substring(1);
}
statsStr=head.substring(1);
}
}catch (IOException e){
e.printStackTrace();
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return "success:"+statsStr+";"+str+";";
}
public List<BookInfo> orderList(List<BookInfo> bookList,String clientMessage){
int orderByInt = clientMessage.indexOf("orderBy");
String substring1 = clientMessage.substring(orderByInt + 8, clientMessage.indexOf(";"));
String[] s = substring1.split(" ");
if (s[0].equals("id")){
Collections.sort(bookList, Comparator.comparingLong(BookInfo::getId));
if (s[1].equals("desc")){
Collections.reverse(bookList);
}
}
if (s[0].equals("name")){
Collections.sort(bookList, Comparator.comparing(BookInfo::getName));
if (s[1].equals("desc")){
Collections.reverse(bookList);
}
}
if (s[0].equals("author")){
Collections.sort(bookList, Comparator.comparing(BookInfo::getAuthor));
if (s[1].equals("desc")){
Collections.reverse(bookList);
}
}
if (s[0].equals("type")){
Collections.sort(bookList, Comparator.comparing(BookInfo::getType));
if (s[1].equals("desc")){
Collections.reverse(bookList);
}
}
if (s[0].equals("country")){
Collections.sort(bookList, Comparator.comparing(BookInfo::getCountry));
if (s[1].equals("desc")){
Collections.reverse(bookList);
}
}
if (s[0].equals("price")){
Collections.sort(bookList, Comparator.comparingDouble(BookInfo::getPrice));
if (s[1].equals("desc")){
Collections.reverse(bookList);
}
}
return bookList;
}
public List<BookInfo> whereList(List<BookInfo> bookInfoList,String clientMessage){
String[] ands = clientMessage.split("and");
List<BookInfo> bookInfos = new ArrayList<>();
boolean whereBoolean=true;
try {
for (BookInfo bookInfo : bookInfoList) {
whereBoolean=true;
for (String and : ands) {
String replace = and.replace(" ", "");
if (replace.contains(">")){
String substring = replace.substring(replace.indexOf(">") + 1).replace(" ","");
if (bookInfo.getPrice()<Double.valueOf(substring)){
whereBoolean = false;
}
}else if(replace.contains("<")){
String substring = replace.substring(replace.indexOf("<") + 1).replace(" ","");
if (bookInfo.getPrice()>Double.valueOf(substring)){
whereBoolean = false;
}
}else{
String[] split = replace.split("=");
Class<? extends BookInfo> aClass = bookInfo.getClass();
Field field = aClass.getDeclaredField(split[0]);
field.setAccessible(true);
String s = field.get(bookInfo).toString();
if (!split[1].equals(s)){
whereBoolean = false;
}
}
}
if (whereBoolean){
bookInfos.add(bookInfo);
}
}
}catch (NoSuchFieldException | IllegalAccessException e){
throw new RuntimeException(e);
}
return bookInfos;
}
public String getBookString(String clientMessage){
List<String> list = new ArrayList<>();
String back="";
List<BookInfo> bookList = getBookList();
int selectInt = clientMessage.indexOf("select");
int fromInt = clientMessage.indexOf("from");
String substring = clientMessage.substring(selectInt + 6, fromInt).replaceAll("\\s+", "");
if (substring.equals("*")){
if (clientMessage.contains("where")){
if (clientMessage.contains("orderBy")){
int whereInt = clientMessage.indexOf("where");
String substring1 = clientMessage.substring(whereInt + 5, clientMessage.indexOf("orderBy")).replace(" ","");
List<BookInfo> bookInfos = whereList(bookList, substring1);
List<BookInfo> bookInfoList = orderList(bookInfos, clientMessage);
back = spliceBookInfo(bookInfoList,substring);
}else{
int whereInt = clientMessage.indexOf("where");
String substring1 = clientMessage.substring(whereInt + 5, clientMessage.indexOf(";")).replace(" ","");
List<BookInfo> bookInfos = whereList(bookList, substring1);
back = spliceBookInfo(bookInfos,substring);
}
}else{
if (clientMessage.contains("orderBy")){
List<BookInfo> bookInfos = orderList(bookList, clientMessage);
back = spliceBookInfo(bookInfos,substring);
}else{
back = spliceBookInfo(bookList,substring);
}
}
}else{
String[] split = substring.split(",");
BookInfo bookInfo = new BookInfo();
for (String s : split) {
if(!hasField(bookInfo,s)){
return "error:属性\""+s+"\",不存在";
}
}
if (clientMessage.contains("where")){
if (clientMessage.contains("orderBy")){
int whereInt = clientMessage.indexOf("where");
String substring1 = clientMessage.substring(whereInt + 5, clientMessage.indexOf("orderBy")).replace(" ","");
List<BookInfo> bookInfos = whereList(bookList, substring1);
List<BookInfo> bookInfoList = orderList(bookInfos, clientMessage);
back = spliceBookInfo(bookInfoList,substring);
}else{
int whereInt = clientMessage.indexOf("where");
String substring1 = clientMessage.substring(whereInt + 5, clientMessage.indexOf(";")).replace(" ","");
List<BookInfo> bookInfos = whereList(bookList, substring1);
back = spliceBookInfo(bookInfos,substring);
}
}else{
if (clientMessage.contains("orderBy")){
bookList = orderList(bookList, clientMessage);
back = spliceBookInfo(bookList,substring);
}else{
back = spliceBookInfo(bookList,substring);
}
}
}
return back;
}
@Override
public void run() {
try {
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// String clientName = clientSocket.getInetAddress().getHostName();
String clientId = br.readLine();
System.out.println("客户端名称:" + clientId);
String clientMessage;
while ((clientMessage = br.readLine()) != null) {
if (clientMessage.equals("exit")) {
System.out.println(clientId + "已断开连接");
break;
}
System.out.println("来自" + clientId + "的消息:" + clientMessage);
// 在这里处理收到的消息,并发送响应给客户端
//判断请求是否为查询
if (clientMessage.endsWith("")){
pw.println("error:违法字符串:\"\"");
continue;
}
if(!clientMessage.endsWith(";")){
pw.println("error:请求必须以\";\"结尾");
continue;
}
if (clientMessage.startsWith("select")){
String bookString = getBookString(clientMessage);
pw.println(bookString);
} else
//判断请求是否为修改
if (clientMessage.startsWith("update")){
pw.println("success:修改成功");
} else
//判断请求是否为删除
if (clientMessage.startsWith("delete")){
pw.println("success:删除成功");
} else
//判断请求是否为添加
if (clientMessage.startsWith("insert")){
pw.println("success:添加成功");
} else {
pw.println("error:请求格式错误");
}
}
br.close();
pw.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,128 @@
package com.bwie.lx;
import com.alibaba.fastjson.JSON;
import com.bwie.lx.doamin.BookInfo;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* @ClassName DTest
* @Description
* @Author Xin.Yao
* @Date 2024/4/15 11:01
*/
/**
*
*/
public class D1Test {
public static List<BookInfo> getBookList(String bookString){
List<BookInfo> bookList = new ArrayList<>();
List<String> list = new ArrayList<>();
String[] split = bookString.split(";");
if (split.length>1){
for (String s : split) {
list.add(s);
}
String handle=list.get(0);
String[] handles = handle.split("\\|");
list.remove(0);
bookList = list.stream()
.map(book -> {
String[] split1 = book.split("\\|");
String s1 = "";
for (int i = 0; i < split1.length; i++) {
s1 = s1+","+"\""+handles[i]+"\""+":"+"\""+split1[i]+"\"";
}
s1=s1.substring(1);
s1="{"+s1+"}";
System.out.println(s1);
return JSON.parseObject(s1,BookInfo.class);
}).collect(Collectors.toList());
}
return bookList;
}
/**
*
* @param args
*/
public static void main(String[] args) throws Exception {
//客户端
Socket client_socket = new Socket("localhost",6666);
//提示
System.out.println("客户端1已启动");
//声明类
Scanner sc = new Scanner(System.in,"UTF-8");
//打印流
PrintWriter pw = new PrintWriter(client_socket.getOutputStream());
// 设置客户端唯一标识
String clientId = "Client1";
//写名称
pw.println(clientId);
//缓冲读流
BufferedReader br = new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
try {
while (true){
/*******************1、先写***************************/
//内容
System.out.println("请输入给服务端发送的信息:");
String server_msg = sc.nextLine();
//写
//pw.println(server_msg);
pw.println(server_msg);
//刷新
pw.flush();
/*******************2、后读***************************/
//读取
String client_msg = br.readLine();
//输出
System.out.println("服务端发来的信息:"+client_msg);
if (client_msg.startsWith("success")){
if (server_msg.startsWith("select")){
client_msg=client_msg.replace("success:","");
List<BookInfo> bookList = getBookList(client_msg);
if (bookList==null){
System.out.println(" 客户端解析:");
}else{
System.out.println(" 客户端解析:"+ bookList);
}
}else{
client_msg=client_msg.replace("success:","");
System.out.println(" 客户端解析:"+ client_msg);
}
}
// /*******************1、先读***************************/
// //读取
// String client_msg = br.readLine();
// //输出
// System.out.println(client_msg);
// /*******************2、后写***************************/
// //内容
// String server_msg = sc.next();
// //写
// pw.println(server_msg);
// //刷新
// pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭
pw.close();
//关闭
br.close();
}
}
}

View File

@ -0,0 +1,101 @@
package com.bwie.lx;
import com.bwie.lx.doamin.BookInfo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* @ClassName DTest
* @Description
* @Author Xin.Yao
* @Date 2024/4/15 11:01
*/
/**
*
*/
public class D2Test {
public static List<BookInfo> getBookList(String bookString){
List<BookInfo> bookList = new ArrayList<>();
List<String> list= Arrays.asList(bookString.split(";"));
list.remove(0);
bookList = list.stream()
.map(book -> {
return new BookInfo().bookInfoBuild(book.split("\\|"));
}).collect(Collectors.toList());
System.out.println(bookList);
return bookList;
}
/**
*
* @param args
*/
public static void main(String[] args) throws Exception {
//客户端
Socket client_socket = new Socket("localhost",6666);
//提示
System.out.println("客户端2已启动");
//声明类
Scanner sc = new Scanner(System.in,"UTF-8");
//打印流
PrintWriter pw = new PrintWriter(client_socket.getOutputStream());
// 设置客户端唯一标识
String clientId = "Client2";
//写名称
pw.println(clientId);
//缓冲读流
BufferedReader br = new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
try {
while (true){
/*******************1、先写***************************/
//内容
System.out.println("请输入给服务端发送的信息:");
String server_msg = sc.next();
//写
//pw.println(server_msg);
pw.println(server_msg);
//刷新
pw.flush();
/*******************2、后读***************************/
//读取
String client_msg = br.readLine();
//输出
System.out.println("服务端发来的信息:"+client_msg);
List<BookInfo> bookList = getBookList(client_msg);
System.out.println(" 客户端解析:"+ bookList);
// /*******************1、先读***************************/
// //读取
// String client_msg = br.readLine();
// //输出
// System.out.println(client_msg);
// /*******************2、后写***************************/
// //内容
// String server_msg = sc.next();
// //写
// pw.println(server_msg);
// //刷新
// pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭
pw.close();
//关闭
br.close();
}
}
}

View File

@ -0,0 +1,13 @@
package com.bwie.lx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LxApplication {
public static void main(String[] args) {
SpringApplication.run(LxApplication.class, args);
}
}

View File

@ -0,0 +1,100 @@
package com.bwie.lx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @ClassName UTest
* @Description
* @Author Xin.Yao
* @Date 2024/4/15 11:02
*/
/**
*
*/
public class UTest {
// //管理多个客户端
// static List<Socket> list = new ArrayList<>();
//
// /**
// *
// * @param args
// */
// public static void main(String[] args) throws Exception {
// //服务端对象
// ServerSocket server = new ServerSocket(6666);
// //提示
// System.out.println("服务端已启动...");
//
// //客户端
// Socket server_socket = server.accept();
// //提示
// System.out.println("客户端已被监听到...");
//
// //声明类
// Scanner sc = new Scanner(System.in,"UTF-8");
//
// //打印流
// PrintWriter pw = new PrintWriter(server_socket.getOutputStream());
// //缓冲读流
// BufferedReader br = new BufferedReader(new InputStreamReader(server_socket.getInputStream()));
//
// try {
// while (true){
// //读取
// String client_msg = br.readLine();
// if (client_msg.equals("exit")){
// System.out.println("客户端已断开连接");
// //关闭
// br.close();
// //关闭
// pw.close();
// return;
// }
// //输出
// System.out.println("客户端发来的信息:"+client_msg);
// /*******************2、后写***************************/
// //内容
// //写
// pw.println("id|name|author|type|country|price");
// //刷新
// pw.flush();
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// //关闭
// br.close();
// //关闭
// pw.close();
// }
// }
public static void main(String[] args) throws IOException {
try {
ServerSocket serverSocket = new ServerSocket(6666);
System.out.println("服务端已启动...");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("客户端已连接:" + clientSocket.getInetAddress().getHostName());
ClientHandler clientHandler = new ClientHandler(clientSocket);
Thread clientThread = new Thread(clientHandler);
clientThread.start();
}
}catch (IOException e){
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,40 @@
package com.bwie.lx.doamin;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @ClassName BookInfo
* @Description
* @Author Xin.Yao
* @Date 2024/4/15 14:08
*/
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class BookInfo {
private Long id;
private String name;
private String author;
private String type;
private String country;
private Double price;
public BookInfo bookInfoBuild(String[] split){
return BookInfo.builder()
.id(Long.valueOf(split[0]))
.name(split[1])
.author(split[2])
.type(split[3])
.country(split[4])
.price(Double.valueOf(split[5]))
.build();
}
public String getString(){
return id+"|"+name+"|"+author+"|"+type+"|"+country+"|"+price;
}
}

View File

@ -0,0 +1,4 @@
id|name|author|type|country|price
1|三国演义|罗贯中|四大名著|中国|19.68
2|java入门到高薪|张艺兴|技术书籍|中国|36.89
3|javaweb|范雅婷|技术书籍|中国|65.32

View File

@ -0,0 +1,7 @@
# 应用服务 WEB 访问端口
server.port=8080
# 设置字符编码为UTF-8
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true

View File

@ -0,0 +1,6 @@
<html>
<body>
<h1>hello word!!!</h1>
<p>this is a html page</p>
</body>
</html>

View File

@ -0,0 +1,13 @@
package com.bwie.lx;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class LxApplicationTests {
@Test
void contextLoads() {
}
}