廖井涛
2024-03-04 eae17d27ec56a6b7887f5597335e38ca40273ef4
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package builder;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URI;
import java.sql.CallableStatement;
import java.sql.Connection;
 
import com.alibaba.fastjson.JSONArray;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
 
import com.alibaba.fastjson.JSONObject;
import ng.db.DBHelper;
import ng.db.DBSession;
 
 
 
 
 
public class HttpHandlerDemo implements HttpHandler{
    DBHelper db;
 
    @Override
    public void handle(HttpExchange httpExchange) throws IOException {
        //ÇëÇóµØÖ·
        InetSocketAddress inetSocketAddress=httpExchange.getRemoteAddress();
        //ÇëÇó·½Ê½
        String requestMethod=httpExchange.getRequestMethod();
        //url
        URI url=httpExchange.getRequestURI();
        if(requestMethod.equalsIgnoreCase("GET")){//¿Í»§¶ËµÄÇëÇóÊÇget·½·¨
            //ÉèÖ÷þÎñ¶ËÏìÓ¦µÄ±àÂë¸ñʽ£¬·ñÔòÔÚ¿Í»§¶ËÊÕµ½µÄ¿ÉÄÜÊÇÂÒÂë
            Headers responseHeaders = httpExchange.getResponseHeaders();
            responseHeaders.set("Content-Type", "text/html;charset=utf-8");
 
            //ÔÚÕâÀïͨ¹ýhttpExchange»ñÈ¡¿Í»§¶Ë·¢Ë͹ýÀ´µÄÏûÏ¢
            //URI url = httpExchange.getRequestURI();
            //InputStream requestBody = httpExchange.getRequestBody();
 
            String response = "this is server";
 
            httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.getBytes("UTF-8").length);
 
            OutputStream responseBody = httpExchange.getResponseBody();
            OutputStreamWriter writer = new OutputStreamWriter(responseBody, "UTF-8");
            writer.write(response);
            writer.close();
            responseBody.close();
        }
        else {
            //ÇëÇó±¨ÎÄ
            InputStream inputStream=httpExchange.getRequestBody();
            ByteArrayOutputStream  bas=new ByteArrayOutputStream();
            int i;
            while((i=inputStream.read())!=-1) {
                bas.write(i);
            }
            String requestmsg=bas.toString();
            JSONObject jsonObject =  JSONObject.parseObject(requestmsg);
 
            String shebei=jsonObject.get("sn").toString();
 
            JSONArray a=jsonObject.getJSONArray("logs");
 
            String user_id=a.getJSONObject(0).get("user_id").toString();
            String times=a.getJSONObject(0).get("recog_time").toString();
            System.out.println("ÇëÇó±¨ÎÄ:"+shebei+user_id+times);
 
            DBSession sn=null;
            DBHelper.addHelper("mes","jdbc:mysql://127.0.0.1:3307/gmms?serverTimezone=GMT%2B8","root","beibo.123/");
            String result=null;
 
            try{
 
                sn=DBHelper.getDBHelper("mes").createSession(false);
                Connection con= sn.getConnection();
                CallableStatement sql=con.prepareCall("{call Face_recognition(?,?,?,?)}");
                sql.registerOutParameter(4, java.sql.Types.VARCHAR);
                sql.setString(1, shebei);
                sql.setString(2, user_id);
                sql.setString(3, times);
                sql.execute();
                result= sql.getString(4);
                System.out.println(result);
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                sn.close();
 
            }
 
            //·µ»Ø±¨ÎÄ
            JSONObject jsonObject1 =  new JSONObject();
            jsonObject1.put("Result", 0);
            jsonObject1.put("Content", "");
            jsonObject1.put("Msg", "");
            httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK,jsonObject1.toString().getBytes("UTF-8").length );
            OutputStream outputStream=httpExchange.getResponseBody();
            outputStream.write(jsonObject1.toString().getBytes("UTF-8"));
            outputStream.close();
        }
    }
 
}