From 59340c0b9107c813d6b3a5c953d693212485ad11 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 20 八月 2025 13:31:22 +0800
Subject: [PATCH] 2025年08月20日13:31:14
---
app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java | 148 ++++++++++++++++++++++++++++++++++++++----------
1 files changed, 116 insertions(+), 32 deletions(-)
diff --git a/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java b/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java
index ae5e6f3..3cc1df8 100644
--- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java
+++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java
@@ -4,9 +4,14 @@
import com.alibaba.fastjson.JSON;
import com.hdl.photovoltaic.other.HdlFileLogic;
+import com.hdl.photovoltaic.other.HdlLogLogic;
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@@ -15,12 +20,12 @@
import fi.iki.elonen.NanoHTTPD;
/**
- * 鏈湴鏈嶅姟鍣�
+ * 鏈湴鏈嶅姟
*/
public class MyNanoHttpServer extends NanoHTTPD {
//澹版槑鏈嶅姟绔� 绔彛
- public static final Integer HTTP_PORT = 49152;
+ public static Integer HTTP_PORT = 49152;// 46219;
public MyNanoHttpServer(String hostname, int port) {
super(hostname, port);
@@ -40,17 +45,21 @@
return myNanoHttpServer;
}
+
@Override
public Response serve(IHTTPSession session) {
//TODO 瑙e喅瀹㈡埛绔姹傚弬鏁版惡甯︿腑鏂囷紝鍑虹幇涓枃涔辩爜闂
ContentType ct = new ContentType(session.getHeaders().get("content-type")).tryUTF8();
session.getHeaders().put("content-type", ct.getContentTypeHeader());
return dealWith(session);
+
}
+
private Response dealWith(IHTTPSession session) {
Date dateTime = new Date();
if (Method.POST == session.getMethod()) {
+ //todo POST鏂规硶
//鑾峰彇璇锋眰澶存暟鎹�
Map<String, String> header = session.getHeaders();
//鑾峰彇浼犲弬鍙傛暟
@@ -58,49 +67,124 @@
try {
session.parseBody(params);
String paramStr = params.get("postData");
- if (TextUtils.isEmpty(paramStr)) {
- return newFixedLengthResponse("success");
- }
- paramStr = paramStr.replace("\r\n", " ");
-
- com.alibaba.fastjson.JSONObject jsonParam = JSON.parseObject(paramStr);
- Map<String, Object> result = new HashMap<>();
- //TODO 鍐欎綘鐨勪笟鍔¢�昏緫.....
- //鍗囩骇椹卞姩鏂囦欢璺緞
- String fileName = ((HTTPSession) session).getUri();
- String data = "";
- if (fileName.equals(HdlFileLogic.getInstance().getDriveRootPath())) {
-
- data = HdlFileLogic.getInstance().readFile(fileName);
-
- }
//鍝嶅簲瀹㈡埛绔�
- return newFixedLengthResponse(data);
-// return newFixedLengthResponse("success");
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ResponseException e) {
+ return newFixedLengthResponse("success");
+ } catch (IOException | ResponseException e) {
e.printStackTrace();
}
return newFixedLengthResponse("success");
} else if (Method.GET == session.getMethod()) {
+ //todo GET鏂规硶
Map<String, List<String>> parameters = session.getParameters();
- //鍗囩骇椹卞姩鏂囦欢璺緞
+ //椹卞姩鍗囩骇鏂囦欢璺緞
String fileName = ((HTTPSession) session).getUri();
- String data = "";
- if (fileName.contains(HdlFileLogic.getInstance().getDriveRootPath())) {
- data = HdlFileLogic.getInstance().readFile(fileName);
+ if (!TextUtils.isEmpty(fileName)) {
+ if (fileName.contains(HdlFileLogic.getInstance().getDriveRootPath())
+ || fileName.contains(HdlFileLogic.getInstance().getFirmwareRootPath())) {
+ String range = session.getHeaders().get("range");
+ if (TextUtils.isEmpty(range)) {
+ return newFixedLengthResponse("閿欒,澶撮儴娌℃湁range瀛楁(-500)");
+ }
+ HdlLogLogic.print("http閫嗗彉鍣ㄨ姹�===璇锋眰澶у皬-->" + range, true);
+ if (!range.contains("=")) {
+ HdlLogLogic.print("閿欒,range鐨剉alue鏍煎紡涓嶅.(-501)", true);
+ return newFixedLengthResponse("閿欒,range鐨剉alue鏍煎紡涓嶅.(-501)");
+ }
+ String[] bytesAry = range.split("=");
+ if (bytesAry.length < 2) {
+ HdlLogLogic.print("閿欒,range鐨剉alue鏍煎紡涓嶅.(-502)", true);
+ return newFixedLengthResponse("閿欒,range鐨剉alue鏍煎紡涓嶅.(-502)");
+ }
+ String[] dataIndex = bytesAry[1].split("-");
+ if (dataIndex.length < 2) {
+ HdlLogLogic.print("閿欒,range鐨剉alue鏍煎紡涓嶅.(-503)", true);
+ return newFixedLengthResponse("閿欒,range鐨剉alue鏍煎紡涓嶅.(-503)");
+ }
+ int startIndex = Integer.parseInt(dataIndex[0]);
+ int endIndex = Integer.parseInt(dataIndex[1]);
+ if (endIndex + 1 - startIndex <= 0) {
+ HdlLogLogic.print("閿欒,璇锋眰澶у皬鏈夐棶棰�.(-504)", true);
+ return newFixedLengthResponse("閿欒,璇锋眰澶у皬鏈夐棶棰�.(-504)");
+ }
+ //璇诲彇鏈湴鏂囦欢瀛楄妭娴�
+ byte[] sourceDataByte = HdlFileLogic.getInstance().readFileByte(fileName);
+ if (sourceDataByte == null || sourceDataByte.length == 0) {
+ HdlLogLogic.print("閿欒,鏈湴鎵句笉鍒版枃浠�,璇蜂笅杞藉啀璇�.(-505)", true);
+ return newFixedLengthResponse("閿欒,鏈湴鎵句笉鍒版枃浠�,璇蜂笅杞藉啀璇�.(-505)");
+ }
+ //鏍规嵁璇锋眰澶у皬鍒涘缓缂撳瓨鍖�
+ byte[] newByte = new byte[endIndex - startIndex + 1];
+ //鏍规嵁璇锋眰鍋忕Щ閲忓彇鏁版嵁,涓斿鍒跺埌缂撳瓨鍖�
+ System.arraycopy(sourceDataByte, startIndex, newByte, 0, endIndex + 1 - startIndex);
+ //鍝嶅簲瀹㈡埛绔�
+ Response response = newFixedLengthResponse(newByte);
+ response.addHeader("Content-Range", range + "/" + sourceDataByte.length);//浼熷崡闇�瑕佽繖涓瓧娈�
+ HdlLogLogic.print("http鍥炲鏁版嵁===璇锋眰澶у皬--" + range + "=====鏂囦欢澶у皬--" + sourceDataByte.length, true);
+ return response;
+ }
}
- //鍝嶅簲瀹㈡埛绔�
- return newFixedLengthResponse(data);
-// return newFixedLengthResponse("success");
- }
+ return newFixedLengthResponse("success");
+ }
return newFixedLengthResponse("404");
}
public static Response newFixedLengthResponse(String msg) {
- return newFixedLengthResponse(Response.Status.OK, NanoHTTPD.MIME_HTML, msg);
+
+ /**
+ 鏂囨湰绫诲瀷锛�
+ text/plain锛氱函鏂囨湰
+ text/html锛欻TML 鏂囨。
+ text/css锛欳SS 鏍峰紡琛�
+ text/javascript锛欽avaScript 鑴氭湰
+ 搴旂敤绋嬪簭绫诲瀷锛�
+ application/json锛欽SON 鏁版嵁
+ application/xml锛歑ML 鏁版嵁
+ application/pdf锛歅DF 鏂囨。
+ application/octet-stream锛氫簩杩涘埗鏁版嵁娴�
+ application/x-www-form-urlencoded锛歎RL 缂栫爜鐨勮〃鍗曟暟鎹�
+ application/zip锛歓IP 鍘嬬缉鏂囦欢
+ application/x-gzip锛欸ZIP 鍘嬬缉鏂囦欢
+ 鍥剧墖绫诲瀷锛�
+ image/jpeg锛欽PEG 鍥惧儚
+ image/png锛歅NG 鍥惧儚
+ image/gif锛欸IF 鍥惧儚
+ image/svg+xml锛歋VG 鍥惧儚
+ 闊抽/瑙嗛绫诲瀷锛�
+ audio/mpeg锛歁P3 闊抽
+ video/mp4锛歁P4 瑙嗛
+ video/mpeg锛歁PEG 瑙嗛
+ */
+ return newFixedLengthResponse(Response.Status.OK, "application/octet-stream", msg);
+ }
+
+ public static Response newFixedLengthResponse(byte[] by) {
+
+ /**
+ 鏂囨湰绫诲瀷锛�
+ text/plain锛氱函鏂囨湰
+ text/html锛欻TML 鏂囨。
+ text/css锛欳SS 鏍峰紡琛�
+ text/javascript锛欽avaScript 鑴氭湰
+ 搴旂敤绋嬪簭绫诲瀷锛�
+ application/json锛欽SON 鏁版嵁
+ application/xml锛歑ML 鏁版嵁
+ application/pdf锛歅DF 鏂囨。
+ application/octet-stream锛氫簩杩涘埗鏁版嵁娴�
+ application/x-www-form-urlencoded锛歎RL 缂栫爜鐨勮〃鍗曟暟鎹�
+ application/zip锛歓IP 鍘嬬缉鏂囦欢
+ application/x-gzip锛欸ZIP 鍘嬬缉鏂囦欢
+ 鍥剧墖绫诲瀷锛�
+ image/jpeg锛欽PEG 鍥惧儚
+ image/png锛歅NG 鍥惧儚
+ image/gif锛欸IF 鍥惧儚
+ image/svg+xml锛歋VG 鍥惧儚
+ 闊抽/瑙嗛绫诲瀷锛�
+ audio/mpeg锛歁P3 闊抽
+ video/mp4锛歁P4 瑙嗛
+ video/mpeg锛歁PEG 瑙嗛
+ */
+ return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "application/octet-stream", new ByteArrayInputStream(by), by.length);
}
}
--
Gitblit v1.8.0