From f2cacfb5c8760d2b9b539821cc9e13054dbc1320 Mon Sep 17 00:00:00 2001 From: jiazx0107@163.com <jiazx0107@163.com> Date: 星期二, 24 十月 2023 19:37:10 +0800 Subject: [PATCH] 四川省网关接口相关2 --- src/main/java/com/fzzy/push/gb2022/HttpClientUtil.java | 122 ++++++++++++++++++++++++++++++++++++---- 1 files changed, 109 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/fzzy/push/gb2022/HttpClientUtil.java b/src/main/java/com/fzzy/push/gb2022/HttpClientUtil.java index b1484ff..f7fb25b 100644 --- a/src/main/java/com/fzzy/push/gb2022/HttpClientUtil.java +++ b/src/main/java/com/fzzy/push/gb2022/HttpClientUtil.java @@ -3,6 +3,7 @@ import com.alibaba.fastjson.JSON; import com.fzzy.api.dto.ResponseDto; +import com.fzzy.gateway.data.GatewayResponse; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; @@ -21,14 +22,15 @@ /** * post formData + * * @param url * @param map * @return * @throws Exception */ @SuppressWarnings("resource") - public static ResponseDto postFormData(String url, Map<String, Object> map) throws Exception { - log.info("---------鎺ュ彛璇锋眰鍦板潃锛�" +url+ "----------鍙傛暟锛�" + JSON.toJSONString(map) +"---------"); + public static ResponseDto postFormData(String url, Map<String, Object> map) throws Exception { + log.info("---------鎺ュ彛璇锋眰鍦板潃锛�" + url + "----------鍙傛暟锛�" + JSON.toJSONString(map) + "---------"); BufferedReader in = null; URL urls = new URL(url); HttpURLConnection connection = null; @@ -47,10 +49,10 @@ connection.setConnectTimeout(20000); connection.setReadTimeout(30000); connection.setRequestMethod("POST"); - if(StringUtils.isNotEmpty(JSESSIONID)){ + if (StringUtils.isNotEmpty(JSESSIONID)) { // connection.setRequestProperty("Cookie", "JSESSIONID="+JSESSIONID); // connection.setRequestProperty(" Set-Cookies", JSESSIONID); - connection.setRequestProperty("Cookie", "JSESSIONID="+JSESSIONID); + connection.setRequestProperty("Cookie", "JSESSIONID=" + JSESSIONID); } map.remove("JSESSIONID"); @@ -76,7 +78,7 @@ // System.out.println(buffer.toString()); outputStream.write(buffer.toString().getBytes()); File file = (File) entry.getValue(); - DataInputStream ins = new DataInputStream(new FileInputStream(file)); + DataInputStream ins = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = ins.read(bufferOut)) != -1) { @@ -102,7 +104,7 @@ while ((line = in.readLine()) != null) { rs += line; } - }else{ + } else { log.error("http鐘舵�侊細" + connection.getResponseCode()); log.error("http娑堟伅锛�" + connection.getResponseMessage()); } @@ -110,7 +112,7 @@ String serverCookies = connection.getHeaderField("Set-Cookie"); log.info("serverCookies:" + serverCookies); - if(serverCookies != null) { + if (serverCookies != null) { String[] cookies = serverCookies.split(";"); @@ -128,19 +130,113 @@ } catch (Exception e) { System.out.println("鍙戠敓寮傚父"); - log.error(e.getMessage(),e); + log.error(e.getMessage(), e); rs = null; - return new ResponseDto(99,e.getMessage()); + return new ResponseDto(99, e.getMessage()); } - log.info("---------鎺ュ彛杩斿洖锛�" + rs +"---------"); - responseDto = JSON.parseObject(rs,ResponseDto.class); - if(responseDto == null ) return new ResponseDto(99,"鎺ュ彛璇锋眰鍙戠敓鏈煡閿欒"); + log.info("---------鎺ュ彛杩斿洖锛�" + rs + "---------"); + responseDto = JSON.parseObject(rs, ResponseDto.class); + if (responseDto == null) return new ResponseDto(99, "鎺ュ彛璇锋眰鍙戠敓鏈煡閿欒"); responseDto.setJSESSIONID(JSESSIONID); return responseDto; } finally { try { outputStream.close(); - if (in != null){ + if (in != null) { + in.close(); + } + } catch (Exception e) { + } + outputStream = null; + if (connection != null) + connection.disconnect(); + connection = null; + } + } + + + public static GatewayResponse pushGateway(String url, Map<String, Object> map) throws Exception { + log.info("---------鎺ュ彛璇锋眰鍦板潃锛�" + url + "----------鍙傛暟锛�" + JSON.toJSONString(map) + "---------"); + + BufferedReader in = null; + URL urls = new URL(url); + HttpURLConnection connection = null; + OutputStream outputStream = null; + String rs = ""; + GatewayResponse responseDto; + try { + connection = (HttpURLConnection) urls.openConnection(); + connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----footfoodapplicationrequestnetwork"); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); + connection.setRequestProperty("Accept", "*/*"); + connection.setRequestProperty("Range", "bytes=" + ""); + connection.setConnectTimeout(20000); + connection.setReadTimeout(30000); + connection.setRequestMethod("POST"); + + StringBuffer buffer = new StringBuffer(); + outputStream = connection.getOutputStream(); + Set<Map.Entry<String, Object>> entries = map.entrySet(); + for (Map.Entry<String, Object> entry : entries) { + // 姣忔閮芥竻绌篵uffer锛岄伩鍏嶅啓鍏ヤ笂娆$殑鏁版嵁 + buffer.delete(0, buffer.length()); + buffer.append("------footfoodapplicationrequestnetwork\r\n"); + Object value = entry.getValue(); + if (!(value instanceof File)) { + buffer.append("Content-Disposition: form-data; name=\""); + buffer.append(entry.getKey()); + buffer.append("\"\r\n\r\n"); + buffer.append(entry.getValue()); + buffer.append("\r\n"); + outputStream.write(buffer.toString().getBytes()); + } else { + buffer.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"; filename=\"" + ((File) entry.getValue()).getName() + "\"\r\n"); + buffer.append("Content-Type: " + "zip" + "\r\n\r\n"); + outputStream.write(buffer.toString().getBytes()); + File file = (File) entry.getValue(); + DataInputStream ins = new DataInputStream(new FileInputStream(file)); + int bytes = 0; + byte[] bufferOut = new byte[1024]; + while ((bytes = ins.read(bufferOut)) != -1) { + outputStream.write(bufferOut, 0, bytes); + } + // 鏂囦欢娴佸悗闈㈡坊鍔犳崲琛岋紝鍚﹀垯鏂囦欢鍚庨潰鐨勪竴涓弬鏁颁細涓㈠け + outputStream.write("\r\n".getBytes()); + } + } + if (entries != null && map.size() > 0) { + buffer.delete(0, buffer.length()); + buffer.append("------footfoodapplicationrequestnetwork--\r\n"); + } + outputStream.write(buffer.toString().getBytes()); + try { + connection.connect(); + if (connection.getResponseCode() == 200) { + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); + String line = ""; + while ((line = in.readLine()) != null) { + rs += line; + } + } else { + log.error("http鐘舵�侊細" + connection.getResponseCode()); + log.error("http娑堟伅锛�" + connection.getResponseMessage()); + } + } catch (Exception e) { + System.out.println("鍙戠敓寮傚父"); + log.error(e.getMessage(), e); + rs = null; + return new GatewayResponse(99, e.getMessage()); + } + log.info("---------鎺ュ彛杩斿洖锛�" + rs + "---------"); + responseDto = JSON.parseObject(rs, GatewayResponse.class); + if (responseDto == null) return new GatewayResponse(99, "鎺ュ彛璇锋眰鍙戠敓鏈煡閿欒"); + return responseDto; + } finally { + try { + outputStream.close(); + if (in != null) { in.close(); } } catch (Exception e) { -- Gitblit v1.9.3