From c1a9eee9c9aa0d3c7592fa75e70243e0297b3a0d Mon Sep 17 00:00:00 2001 From: fhx <308050795@qq.com> Date: 星期四, 06 四月 2023 10:38:03 +0800 Subject: [PATCH] 1.新增文件工具类 --- src/main/java/com/hx/util/HttpUtil.java | 126 +++++++++++++++++++++++++++++++++++++++-- 1 files changed, 119 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/hx/util/HttpUtil.java b/src/main/java/com/hx/util/HttpUtil.java index 26caf6f..64b77f6 100644 --- a/src/main/java/com/hx/util/HttpUtil.java +++ b/src/main/java/com/hx/util/HttpUtil.java @@ -3,12 +3,17 @@ import net.sf.json.JSONException; import net.sf.json.JSONObject; import org.apache.commons.io.IOUtils; +import org.bouncycastle.util.encoders.UTF8; import org.springframework.web.multipart.MultipartFile; import javax.activation.MimetypesFileTypeMap; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -23,6 +28,27 @@ /**鍥炶溅鎹㈣,鐢ㄤ簬涓�琛岀殑缁撳熬*/ private static final String LINE_END = "\r\n"; + public static String getInputStream(HttpServletRequest request) throws Exception { + ServletInputStream stream = null; + BufferedReader reader = null; + StringBuffer sb = new StringBuffer(); + try { + stream = request.getInputStream(); + // 鑾峰彇鍝嶅簲 + reader = new BufferedReader(new InputStreamReader(stream)); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + } + } catch (IOException e) { + //logger.error(e); + throw new RuntimeException("璇诲彇杩斿洖鏀粯鎺ュ彛鏁版嵁娴佸嚭鐜板紓甯革紒"); + } finally { + reader.close(); + } + //logger.info("杈撳叆娴佽繑鍥炵殑鍐呭锛�" + sb.toString()); + return sb.toString(); + } public static String post(String requestUrl, String accessToken, String params) throws Exception { @@ -83,6 +109,98 @@ } in.close(); System.err.println("result:" + result); + return result; + } + + /** + * 甯eader鐨刾ost璇锋眰 + * @param generalUrl + * @param header + * @param params + * @return + * @throws Exception + */ + public static String post(String generalUrl, Map<String, String> header, String params) + throws Exception { + URL url = new URL(generalUrl); + // 鎵撳紑鍜孶RL涔嬮棿鐨勮繛鎺� + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + // 璁剧疆閫氱敤鐨勮姹傚睘鎬� + connection.setRequestProperty("Connection", "Keep-Alive"); + if(header != null) + { + for(String key : header.keySet()) + { + connection.setRequestProperty(key, header.get(key)); + } + } + + connection.setUseCaches(false); + connection.setDoOutput(true); + connection.setDoInput(true); + + // 寰楀埌璇锋眰鐨勮緭鍑烘祦瀵硅薄 + DataOutputStream out = new DataOutputStream(connection.getOutputStream()); + out.write(params.getBytes("UTF-8")); + out.flush(); + out.close(); + + // 寤虹珛瀹為檯鐨勮繛鎺� + connection.connect(); + // 瀹氫箟 BufferedReader杈撳叆娴佹潵璇诲彇URL鐨勫搷搴� + BufferedReader in = null; + in = new BufferedReader( + new InputStreamReader(connection.getInputStream(), "UTF-8")); + String result = ""; + String getLine; + while ((getLine = in.readLine()) != null) { + result += getLine; + } + in.close(); + + return result; + } + + /** + * 甯eader鐨刧et璇锋眰 + * @param generalUrl + * @param header + * @return + * @throws Exception + */ + public static String get(String generalUrl, Map<String, String> header) + throws Exception { + URL url = new URL(generalUrl); + // 鎵撳紑鍜孶RL涔嬮棿鐨勮繛鎺� + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + // 璁剧疆閫氱敤鐨勮姹傚睘鎬� + connection.setRequestProperty("Connection", "Keep-Alive"); + if(header != null) + { + for(String key : header.keySet()) + { + connection.setRequestProperty(key, header.get(key)); + } + } + + connection.setUseCaches(false); + connection.setDoOutput(true); + connection.setDoInput(true); + + // 寤虹珛瀹為檯鐨勮繛鎺� + connection.connect(); + // 瀹氫箟 BufferedReader杈撳叆娴佹潵璇诲彇URL鐨勫搷搴� + BufferedReader in = null; + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); + String result = ""; + String getLine; + while ((getLine = in.readLine()) != null) { + result += getLine; + } + in.close(); + return result; } @@ -162,13 +280,6 @@ // //System.out.println("HttpURLUtil:"+wxMsgXml); } catch (Exception e) { e.printStackTrace(); - obj = new JSONObject(); - try { - obj.put("status", 1); - obj.put("errMsg", e.getMessage()); - } catch (JSONException e1) { - e1.printStackTrace(); - } } finally { if (con != null) { con.disconnect(); @@ -543,4 +654,5 @@ } return res; } + } -- Gitblit v1.8.0