| | |
| | | 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; |
| | |
| | | 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); |
| | |
| | | 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) { |
| | |
| | | } |
| | | 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:HTML 文档 |
| | | text/css:CSS 样式表 |
| | | text/javascript:JavaScript 脚本 |
| | | 应用程序类型: |
| | | application/json:JSON 数据 |
| | | application/xml:XML 数据 |
| | | application/pdf:PDF 文档 |
| | | application/octet-stream:二进制数据流 |
| | | application/x-www-form-urlencoded:URL 编码的表单数据 |
| | | application/zip:ZIP 压缩文件 |
| | | application/x-gzip:GZIP 压缩文件 |
| | | 图片类型: |
| | | image/jpeg:JPEG 图像 |
| | | image/png:PNG 图像 |
| | | image/gif:GIF 图像 |
| | | image/svg+xml:SVG 图像 |
| | | 音频/视频类型: |
| | | audio/mpeg:MP3 音频 |
| | | video/mp4:MP4 视频 |
| | | video/mpeg:MPEG 视频 |
| | | */ |
| | | // 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); |
| | | } |
| | | |
| | | } |