New file |
| | |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | } |