diff --git a/src/main/java/com/syjiaer/clinic/server/common/api/request/SocialRequest.java b/src/main/java/com/syjiaer/clinic/server/common/api/request/SocialRequest.java index 98755ad..b465c5c 100644 --- a/src/main/java/com/syjiaer/clinic/server/common/api/request/SocialRequest.java +++ b/src/main/java/com/syjiaer/clinic/server/common/api/request/SocialRequest.java @@ -193,11 +193,11 @@ public class SocialRequest { } public JSONObject call9001(IM9001 im9001){ - return httpUtil.call("9001","signIn",im9001.buildToMap()); + return httpUtil.call("9001","signIn",im9001.buildToMap(),10000); } public JSONObject call9002(IM9002 im9002) { - return httpUtil.call("9002", "signOut", im9002.buildToMap()); + return httpUtil.call("9002", "signOut", im9002.buildToMap(),10000); } diff --git a/src/main/java/com/syjiaer/clinic/server/common/util/HttpUtil.java b/src/main/java/com/syjiaer/clinic/server/common/util/HttpUtil.java index 7794973..d3aed5c 100644 --- a/src/main/java/com/syjiaer/clinic/server/common/util/HttpUtil.java +++ b/src/main/java/com/syjiaer/clinic/server/common/util/HttpUtil.java @@ -38,35 +38,28 @@ public class HttpUtil { private ManagerUserSignMapper managerUserSignMapper; private Logger logger = Logger.getLogger(HttpUtil.class.getName()); - - private JSONObject post(String posturl, String params,Boolean isLog) { + private JSONObject post(String posturl, String params){ + return post(posturl, params, 120000); + } + private JSONObject post(String posturl, String params,int timeout) { String result = ""; try { StringBuffer resultBuffer = new StringBuffer(); logger.info("调用医保请求入参==》" + params); URL url = new URL(posturl); URLConnection conn = url.openConnection(); - - //2.处理设置参数和一般请求属性 - //2.1设置参数 - //可以根据请求的需要设置参数 - conn.setDoInput(true); //默认为true 所以不设置也可以 - conn.setDoOutput(true); //默认为false 发送post请求必须设置setDoOutput(true) - conn.setUseCaches(false); //是否可以使用缓存 不使用缓存 - conn.setConnectTimeout(120000);//请求超时时间 - - //2.2请求属性 - //设置通用的请求属性 消息报头 即设置头字段 更多的头字段信息可以查阅HTTP协议 + conn.setDoInput(true); + conn.setDoOutput(true); + conn.setUseCaches(false); + conn.setConnectTimeout(timeout);//请求超时时间 conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "application/json"); - //2.3设置请求正文 即要提交的数据 PrintWriter pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream())); pw.print(params); pw.flush(); pw.close(); - //3.使用 connect 方法建立到远程对象的实际连接。 conn.connect(); //4.远程对象变为可用。远程对象的头字段和内容变为可访问。 @@ -80,7 +73,7 @@ public class HttpUtil { reader.close(); result = resultBuffer.toString(); } catch (Exception e) { - e.printStackTrace(); + logger.warning("调用医保接口异常:" + e.getMessage()); } logger.info("调用医保请求出参==》" + result); JSONObject result_json = JSON.parseObject(result); @@ -89,7 +82,6 @@ public class HttpUtil { throw new MessageException(result_json.getString("err_msg")); } JSONObject output = result_json.getJSONObject("output"); - //清理一周前记录 return output; } @@ -119,7 +111,7 @@ public class HttpUtil { input_dw.put("file_qury_no", file_qury_no); input_dw.put("fixmedins_code", fixmedins_code); input_dw.put("filename", filename); - System.out.println("文件地址:" + path + "/" + filename); + logger.info("开始下载文件:" + filename); callToFile("9102", "fsDownloadIn", input_dw, path + "/" + filename); List file_list = fileUtil.unzip(path, filename); List read_list = fileUtil.readToList(file_list); @@ -159,57 +151,7 @@ public class HttpUtil { return jsonArray; } - private JSONArray postArray(String posturl, String params) { - String result = ""; - try { - StringBuffer resultBuffer = new StringBuffer(); - logger.info("调用医保请求入参==》" + params); - URL url = new URL(posturl); - URLConnection conn = url.openConnection(); - //2.处理设置参数和一般请求属性 - //2.1设置参数 - //可以根据请求的需要设置参数 - conn.setDoInput(true); //默认为true 所以不设置也可以 - conn.setDoOutput(true); //默认为false 发送post请求必须设置setDoOutput(true) - conn.setUseCaches(false); //是否可以使用缓存 不使用缓存 - conn.setConnectTimeout(120000);//请求超时时间 - - //2.2请求属性 - //设置通用的请求属性 消息报头 即设置头字段 更多的头字段信息可以查阅HTTP协议 - conn.setRequestProperty("Connection", "Keep-Alive"); - conn.setRequestProperty("Content-Type", "application/json"); - - //2.3设置请求正文 即要提交的数据 - PrintWriter pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream())); - pw.print(params); - pw.flush(); - pw.close(); - - //3.使用 connect 方法建立到远程对象的实际连接。 - conn.connect(); - - //4.远程对象变为可用。远程对象的头字段和内容变为可访问。 - BufferedReader reader = null; - String tempLine = null; - - reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK")); - while ((tempLine = reader.readLine()) != null) { - resultBuffer.append(tempLine); - } - reader.close(); - result = resultBuffer.toString(); - } catch (Exception e) { - e.printStackTrace(); - } - logger.info("调用医保请求出参==》" + result); - JSONObject result_json = JSON.parseObject(result); - if (!result_json.getString("err_msg").equals("success") && !result_json.getString("err_msg").isEmpty()) { - throw new MessageException(result_json.getString("err_msg")); - } - JSONArray output = result_json.getJSONArray("output"); - return output; - } private String download(String posturl, String params, String saveFilePath) { StringBuffer resultBuffer = new StringBuffer(); @@ -260,37 +202,30 @@ public class HttpUtil { public JSONObject call(String infno,Map input) { - JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, input)),true); + JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, input))); return output; } public JSONObject call(String infno, String tag, Map input) { - JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)),true); + JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)),120000); + return output; + } + public JSONObject call(String infno, String tag, Map input,int timeout) { + + JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)),timeout); return output; } public JSONObject callBackMsgId(String infno, String tag, Map input) { - JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)),true); + JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input))); return output; } public JSONObject callWithOutLog(String infno, String tag, Map input) { - JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)), false); + JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input))); return output; } - public JSONObject callMutiply(String infno, String tag, Map input, Map inputs) { - Map map = getparms(infno, tag, input); - @SuppressWarnings("unchecked") - Map inputMap = (Map)map.get("input"); - for (String key : inputs.keySet()){ - inputMap.put(key, inputs.get(key)); - } - JSONObject output = post(config.get("social", "url"), JSON.toJSONString(map),true); - return output; - } - - public void callToFile(String infno, String tag, Map input, String saveFilePath) { download(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)), saveFilePath); } @@ -342,79 +277,6 @@ public class HttpUtil { return map; } - - - -// private JSONObject uploadFile(String posturl, String filePath) { -// String result = ""; -// try { -// URL url = new URL(posturl); -// URLConnection conn = url.openConnection(); -// -// // 设置请求属性 -// conn.setDoInput(true); -// conn.setDoOutput(true); -// conn.setUseCaches(false); -// conn.setConnectTimeout(120000); -// conn.setRequestProperty("Connection", "Keep-Alive"); -// conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); -// -// // 写入请求正文 -// try (OutputStream os = conn.getOutputStream()) { -// // 读取文件字节数组 -// byte[] fileBytes = Files.readAllBytes(Paths.get(filePath)); -// String fileName = Paths.get(filePath).getFileName().toString(); -// -// // 写入文件部分 -// os.write(("--" + boundary + "\r\n").getBytes()); -// os.write(("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n").getBytes()); -// os.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes()); -// -// os.write(fileBytes); -// os.write(("\r\n--" + boundary + "\r\n").getBytes()); -// IM9101 im9101 = new IM9101(); -// IM9101.Data data = new IM9101.Data(); -// data.setIn(fileBytes); -// data.setFilename(fileName); -// data.setFixmedinsCode(config.get("social", "fixmedinsCode")); -// im9101.setData(data); -// Map parms = getparms("9101", im9101.buildToMap()); -// // 构建JSON参数部分 -// String jsonString = JSON.toJSONString(parms); -// logger.info("调用医保请求入参==》" + parms); -// // 写入JSON参数部分 -// os.write(("Content-Disposition: form-data; name=\"params\"\r\n").getBytes()); -// os.write(("Content-Type: application/json\r\n\r\n").getBytes()); -// os.write((jsonString + "\r\n").getBytes()); -// -// os.write(("--" + boundary + "--\r\n").getBytes()); -// } -// -//// // 获取响应 -//// try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"))) { -//// String tempLine; -//// StringBuilder resultBuffer = new StringBuilder(); -//// while ((tempLine = reader.readLine()) != null) { -//// resultBuffer.append(tempLine); -//// } -//// result = resultBuffer.toString(); -//// } -// } catch (Exception e) { -// e.printStackTrace(); -// } -// logger.info("调用医保请求出参==》" + result); -// JSONObject result_json = JSON.parseObject(result); -// -// if (!result_json.getString("err_msg").equals("成功") && !result_json.getString("err_msg").equals("success") && !result_json.getString("err_msg").isEmpty()) { -// throw new MessageException(result_json.getString("err_msg")); -// } -// JSONObject output = result_json.getJSONObject("output"); -// return output; -// } - - private static final String boundary = UUID.randomUUID().toString(); - - private JSONObject uploadFile(String posturl, String filePath) { // 写入请求正文 @@ -434,7 +296,7 @@ public class HttpUtil { // 构建JSON参数部分 String jsonString = JSON.toJSONString(parms); // 写入JSON参数部分 - JSONObject result = post(posturl, jsonString, true); + JSONObject result = post(posturl, jsonString); return result; } catch (Exception e) {