mac
2023-11-20 734babb3a7348050fdffe845c560ba8b0b218152
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.hdl.photovoltaic.internet.HttpServer;
 
import android.text.TextUtils;
 
import com.alibaba.fastjson.JSON;
import com.hdl.photovoltaic.other.HdlFileLogic;
 
 
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import fi.iki.elonen.NanoHTTPD;
 
/**
 * 本地服务器
 */
public class MyNanoHttpServer extends NanoHTTPD {
 
    //声明服务端 端口
    private static final Integer HTTP_PORT = 49152;
 
    public MyNanoHttpServer(String hostname, int port) {
        super(hostname, port);
    }
 
    private volatile static MyNanoHttpServer myNanoHttpServer;
 
    //TODO    单例模式,获取实例对象,并传入当前机器IP
    public static MyNanoHttpServer getInstance(String ipAddress) {
        if (myNanoHttpServer == null) {
            synchronized (MyNanoHttpServer.class) {
                if (myNanoHttpServer == null) {
                    myNanoHttpServer = new MyNanoHttpServer(ipAddress, HTTP_PORT);
                }
            }
        }
        return myNanoHttpServer;
    }
 
    @Override
    public Response serve(IHTTPSession session) {
        //TODO  解决客户端请求参数携带中文,出现中文乱码问题
        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()) {
            //获取请求头数据
            Map<String, String> header = session.getHeaders();
            //获取传参参数
            Map<String, String> params = new HashMap<String, String>();
            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 ss = HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFilePath());
                //响应客户端
                return newFixedLengthResponse(ss);
//                return newFixedLengthResponse("success");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ResponseException e) {
                e.printStackTrace();
            }
            return newFixedLengthResponse("success");
        } else if (Method.GET == session.getMethod()) {
 
            Map<String, List<String>> parameters = session.getParameters();
            String ss = HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFilePath());
            //响应客户端
            return newFixedLengthResponse(ss);
//            return newFixedLengthResponse("success");
        }
 
        return newFixedLengthResponse("404");
    }
 
    public static Response newFixedLengthResponse(String msg) {
        return newFixedLengthResponse(Response.Status.OK, NanoHTTPD.MIME_HTML, msg);
    }
 
}