From 4b8bca31c782e1a854e07a8ab021ad2af552c251 Mon Sep 17 00:00:00 2001 From: jianjian Date: Mon, 22 May 2023 16:10:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djava=E7=89=88=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E6=97=A0=E6=B3=95=E9=AA=8C=E8=AF=81=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ServerConfig.json | 43 +++++++++++++++ .../jja8/newbinggogo/NewBingGoGoServer.java | 16 +++++- .../java/cn/jja8/newbinggogo/WebWork.java | 55 ++++++++++++++++--- 3 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 ServerConfig.json diff --git a/ServerConfig.json b/ServerConfig.json new file mode 100644 index 00000000..28524350 --- /dev/null +++ b/ServerConfig.json @@ -0,0 +1,43 @@ +{ + "h1": "NewBingGoGo", + "h2": "简单开始和NewBing聊天", + "p":"", + "firstMessages":[ + "好的,我已清理好板子,可以重新开始了。我可以帮助你探索什么?", + "明白了,我已经抹去了过去,专注于现在。我们现在应该探索什么?", + "重新开始总是很棒。问我任何问题!", + "好了,我已经为新的对话重置了我的大脑。你现在想聊些什么?", + "很好,让我们来更改主题。你在想什么?", + "谢谢你帮我理清头绪! 我现在能帮你做什么?", + "没问题,很高兴你喜欢上一次对话。让我们转到一个新主题。你想要了解有关哪些内容的详细信息?", + "谢谢你! 知道你什么时候准备好继续前进总是很有帮助的。我现在能为你回答什么问题?", + "当然,我已准备好进行新的挑战。我现在可以为你做什么?" + ], + "firstProposes":[ + "教我一个新单词", + "我需要有关家庭作业的帮助", + "我想学习一项新技能", + "最深的海洋是哪个?", + "一年有多少小时?", + "宇宙是如何开始的?", + "寻找非虚构作品", + "火烈鸟为何为粉色?", + "有什么新闻?", + "让我大笑", + "给我看鼓舞人心的名言", + "世界上最小的哺乳动物是什么?", + "向我显示食谱", + "最深的海洋是哪个?", + "为什么人类需要睡眠?", + "教我有关登月的信息", + "我想学习一项新技能", + "如何创建预算?", + "给我说个笑话", + "全息影像的工作原理是什么?", + "如何设定可实现的目标?", + "金字塔是如何建成的?", + "激励我!", + "宇宙是如何开始的?", + "如何制作蛋糕?" + ] +} \ No newline at end of file diff --git a/src/main/java/cn/jja8/newbinggogo/NewBingGoGoServer.java b/src/main/java/cn/jja8/newbinggogo/NewBingGoGoServer.java index ccf61a9a..2d8922d3 100644 --- a/src/main/java/cn/jja8/newbinggogo/NewBingGoGoServer.java +++ b/src/main/java/cn/jja8/newbinggogo/NewBingGoGoServer.java @@ -15,6 +15,7 @@ import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.function.BiConsumer; public class NewBingGoGoServer extends NanoWSD { ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); @@ -28,7 +29,7 @@ public static void main(String[] args) { .load(new File("Cookies.yml")) .as(Config.class) .save(new File("Cookies.yml")); - + WebWork.init(); //启动 try{ int porint = Integer.parseInt(args[0]); @@ -255,6 +256,8 @@ public static Response goUrl(IHTTPSession session, String stringUrl, Map> responseHeader = urlConnection.getHeaderFields(); + //将数据全部读取然后关闭流和链接 int len = urlConnection.getContentLength(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(Math.max(len, 0)); @@ -280,6 +283,17 @@ public static Response goUrl(IHTTPSession session, String stringUrl, Map { + if(s!=null + && !s.equals("Content-Length") + && !s.equals("Content-Type") + ){ + for (String string : strings) { + response.addHeader(s, string); + } + } + }); response.addHeader("cookieID", String.valueOf(cookieID)); return response; } diff --git a/src/main/java/cn/jja8/newbinggogo/WebWork.java b/src/main/java/cn/jja8/newbinggogo/WebWork.java index 9e2adeb0..84915caf 100644 --- a/src/main/java/cn/jja8/newbinggogo/WebWork.java +++ b/src/main/java/cn/jja8/newbinggogo/WebWork.java @@ -2,19 +2,38 @@ import fi.iki.elonen.NanoHTTPD; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.URL; public class WebWork { + static File serverConfig; + static void init(){ + File serverConfig = new File("ServerConfig.json"); + try { + URL url = WebWork.class.getClassLoader().getResource("web/resource/config.json"); + if(url!=null && serverConfig.createNewFile()){ + try (InputStream in = url.openStream()){ + try(OutputStream out = new FileOutputStream(serverConfig)){ + byte[] buff = new byte[1024]; + int len; + while ((len = in.read(buff))>=0){ + out.write(buff,0,len); + } + } + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + if (serverConfig.isFile()&&serverConfig.canRead()) { + WebWork.serverConfig = serverConfig; + } + } public static NanoHTTPD.Response getFile(String path) { if(path.startsWith("/")){ path = path.substring(1); } - URL url = WebWork.class.getClassLoader().getResource(path); - if(url==null){ - return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND,"text/html; charset=utf-8","页面不存在!"); - } + //文件属性 String mimeType = "text/html; charset=utf-8"; if(path.endsWith(".html")){ @@ -28,8 +47,30 @@ public static NanoHTTPD.Response getFile(String path) { } else if (path.endsWith(".ico")) { mimeType = "image/png"; } + + + InputStream inputStream; + if(serverConfig!=null && path.equals("web/resource/config.json")){ + try { + NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse( + NanoHTTPD.Response.Status.OK, + mimeType, + new FileInputStream(serverConfig), + -1 + ); + response.addHeader("cache-control","max-age=14400"); + return response; + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } + + URL url = WebWork.class.getClassLoader().getResource(path); + if(url==null){ + return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND,"text/html; charset=utf-8","页面不存在!"); + } try{ - InputStream inputStream = url.openStream(); + inputStream = url.openStream(); NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse( NanoHTTPD.Response.Status.OK, mimeType,