Compare commits

..

No commits in common. "d0cb4d6a62f831a8e5280f03b949731666cbccf0" and "740af771d44a865fd99e255d5d0db8b4227b5337" have entirely different histories.

2 changed files with 160 additions and 22 deletions

View File

@ -193,11 +193,11 @@ public class SocialRequest {
}
public JSONObject call9001(IM9001 im9001){
return httpUtil.call("9001","signIn",im9001.buildToMap(),10000);
return httpUtil.call("9001","signIn",im9001.buildToMap());
}
public JSONObject call9002(IM9002 im9002) {
return httpUtil.call("9002", "signOut", im9002.buildToMap(),10000);
return httpUtil.call("9002", "signOut", im9002.buildToMap());
}

View File

@ -38,28 +38,35 @@ public class HttpUtil {
private ManagerUserSignMapper managerUserSignMapper;
private Logger logger = Logger.getLogger(HttpUtil.class.getName());
private JSONObject post(String posturl, String params){
return post(posturl, params, 120000);
}
private JSONObject post(String posturl, String params,int timeout) {
private JSONObject post(String posturl, String params,Boolean isLog) {
String result = "";
try {
StringBuffer resultBuffer = new StringBuffer();
logger.info("调用医保请求入参==》" + params);
URL url = new URL(posturl);
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(timeout);//请求超时时间
//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.远程对象变为可用远程对象的头字段和内容变为可访问
@ -73,7 +80,7 @@ public class HttpUtil {
reader.close();
result = resultBuffer.toString();
} catch (Exception e) {
logger.warning("调用医保接口异常:" + e.getMessage());
e.printStackTrace();
}
logger.info("调用医保请求出参==》" + result);
JSONObject result_json = JSON.parseObject(result);
@ -82,6 +89,7 @@ public class HttpUtil {
throw new MessageException(result_json.getString("err_msg"));
}
JSONObject output = result_json.getJSONObject("output");
//清理一周前记录
return output;
}
@ -111,7 +119,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);
logger.info("开始下载文件:" + filename);
System.out.println("文件地址:" + path + "/" + filename);
callToFile("9102", "fsDownloadIn", input_dw, path + "/" + filename);
List<String> file_list = fileUtil.unzip(path, filename);
List<String> read_list = fileUtil.readToList(file_list);
@ -151,7 +159,57 @@ 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();
@ -202,30 +260,37 @@ public class HttpUtil {
public JSONObject call(String infno,Map<String, Object> input) {
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, input)));
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, input)),true);
return output;
}
public JSONObject call(String infno, String tag, Map<String, Object> input) {
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)),120000);
return output;
}
public JSONObject call(String infno, String tag, Map<String, Object> input,int timeout) {
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)),timeout);
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)),true);
return output;
}
public JSONObject callBackMsgId(String infno, String tag, Map<String, Object> input) {
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)));
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)),true);
return output;
}
public JSONObject callWithOutLog(String infno, String tag, Map<String, Object> input) {
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)));
JSONObject output = post(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)), false);
return output;
}
public JSONObject callMutiply(String infno, String tag, Map<String, Object> input, Map<String, Object> inputs) {
Map<String, Object> map = getparms(infno, tag, input);
@SuppressWarnings("unchecked")
Map<String, Object> inputMap = (Map<String, Object>)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<String, Object> input, String saveFilePath) {
download(config.get("social", "url"), JSON.toJSONString(getparms(infno, tag, input)), saveFilePath);
}
@ -277,6 +342,79 @@ 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<String, Object> 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) {
// 写入请求正文
@ -296,7 +434,7 @@ public class HttpUtil {
// 构建JSON参数部分
String jsonString = JSON.toJSONString(parms);
// 写入JSON参数部分
JSONObject result = post(posturl, jsonString);
JSONObject result = post(posturl, jsonString, true);
return result;
} catch (Exception e) {