From b2d2e6dcf6ec38a2f24721f2bc925d634a6eb78d Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期二, 05 十二月 2023 18:31:01 +0800 Subject: [PATCH] 2023年12月05日18:30:40 --- app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 46 insertions(+), 12 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 7653899..9601d04 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 @@ -6,6 +6,8 @@ import com.hdl.photovoltaic.other.HdlFileLogic; +import java.io.BufferedInputStream; +import java.io.FileInputStream; import java.io.IOException; import java.util.Date; import java.util.HashMap; @@ -15,12 +17,12 @@ import fi.iki.elonen.NanoHTTPD; /** - * 鏈湴鏈嶅姟鍣� + * 鏈湴鏈嶅姟 */ public class MyNanoHttpServer extends NanoHTTPD { //澹版槑鏈嶅姟绔� 绔彛 - private static final Integer HTTP_PORT = 49152; + public static final Integer HTTP_PORT = 49152; public MyNanoHttpServer(String hostname, int port) { super(hostname, port); @@ -66,11 +68,14 @@ com.alibaba.fastjson.JSONObject jsonParam = JSON.parseObject(paramStr); Map<String, Object> result = new HashMap<>(); //TODO 鍐欎綘鐨勪笟鍔¢�昏緫..... - - String ss = HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFilePath()); + //鍗囩骇椹卞姩鏂囦欢璺緞 + String fileName = ((HTTPSession) session).getUri(); + String data = ""; + if (fileName.equals(HdlFileLogic.getInstance().getDriveRootPath())) { + data = HdlFileLogic.getInstance().readFile(fileName); + } //鍝嶅簲瀹㈡埛绔� - return newFixedLengthResponse(ss); -// return newFixedLengthResponse("success"); + return newFixedLengthResponse(data); } catch (IOException e) { e.printStackTrace(); } catch (ResponseException e) { @@ -78,19 +83,48 @@ } return newFixedLengthResponse("success"); } else if (Method.GET == session.getMethod()) { - Map<String, List<String>> parameters = session.getParameters(); - String ss = HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFilePath()); + //鍗囩骇椹卞姩鏂囦欢璺緞 + String fileName = ((HTTPSession) session).getUri(); + String data = ""; + if (fileName.contains(HdlFileLogic.getInstance().getDriveRootPath())) { + data = HdlFileLogic.getInstance().readFile(fileName); + } //鍝嶅簲瀹㈡埛绔� - return newFixedLengthResponse(ss); -// return newFixedLengthResponse("success"); + return newFixedLengthResponse(data); } - 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 瑙嗛 + */ +// NanoHTTPD.Response response=new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "application/zip",inputStream,inputStream.available()); + return newFixedLengthResponse(Response.Status.OK, "application/octet-stream", msg); +// return newFixedLengthResponse(Response.Status.OK, "application/zip", msg); } } -- Gitblit v1.8.0