From 73bb5617e6aeb4ee8f7dfd4ad54b61d477969d86 Mon Sep 17 00:00:00 2001 From: whycxzp <glperry@163.com> Date: 星期二, 10 六月 2025 20:22:06 +0800 Subject: [PATCH] 自动充放电自动化流程 --- src/main/java/com/whyc/util/HttpUtil.java | 241 +++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 206 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/whyc/util/HttpUtil.java b/src/main/java/com/whyc/util/HttpUtil.java index b3f9f8d..fc979a3 100644 --- a/src/main/java/com/whyc/util/HttpUtil.java +++ b/src/main/java/com/whyc/util/HttpUtil.java @@ -3,6 +3,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; +import com.whyc.dto.Response; +import com.whyc.dto.Response4Http; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -10,9 +12,15 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import java.io.*; import java.lang.reflect.Type; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Map; public class HttpUtil { @@ -49,49 +57,212 @@ return getSession().getServletContext(); } - /* - * 鑾峰彇灏嗗璞¤浆鎹㈡垚json鏍煎紡 + /** + * get鏂瑰紡鐨刪ttp璇锋眰 + * + * @param httpUrl 璇锋眰鍦板潃 + * @return 杩斿洖缁撴灉 */ - public static String toJson(Object obj){ - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); - return gson.toJson(obj); - } - - public static String chageDateToString(Date time,String type){ - String msg = ""; - SimpleDateFormat sdf = new SimpleDateFormat(type); - msg = sdf.format(time); - return msg; + public static Response<String> doGet(String httpUrl, String queryParams) { + HttpURLConnection connection = null; + InputStream inputStream = null; + BufferedReader bufferedReader = null; + String result = null;// 杩斿洖缁撴灉瀛楃涓� + Response<String> response = new Response<>(); + try { + // 鍒涘缓杩滅▼url杩炴帴瀵硅薄 + URL url = new URL(new StringBuffer(httpUrl).append("?").append(queryParams).toString()); + // 閫氳繃杩滅▼url杩炴帴瀵硅薄鎵撳紑涓�涓繛鎺ワ紝寮鸿浆鎴恏ttpURLConnection绫� + connection = (HttpURLConnection) url.openConnection(); + // 璁剧疆杩炴帴鏂瑰紡锛歡et + connection.setRequestMethod("GET"); + // 璁剧疆杩炴帴涓绘満鏈嶅姟鍣ㄧ殑瓒呮椂鏃堕棿锛�15000姣 + connection.setConnectTimeout(5000); + // 璁剧疆璇诲彇杩滅▼杩斿洖鐨勬暟鎹椂闂达細60000姣 + connection.setReadTimeout(6000); + // 鍙戦�佽姹� + connection.connect(); + // 閫氳繃connection杩炴帴锛岃幏鍙栬緭鍏ユ祦 + if (connection.getResponseCode() == 200) { + inputStream = connection.getInputStream(); + // 灏佽杈撳叆娴侊紝骞舵寚瀹氬瓧绗﹂泦 + bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); + // 瀛樻斁鏁版嵁 + StringBuilder sbf = new StringBuilder(); + String temp; + while ((temp = bufferedReader.readLine()) != null) { + sbf.append(temp); + sbf.append(System.getProperty("line.separator")); + } + result = sbf.toString(); + response.set(1,result); + } + } catch (IOException e) { + response.set(0); + String message = e.getMessage(); + response.setMsg(message); + } finally { + // 鍏抽棴璧勬簮 + if (null != bufferedReader) { + try { + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (null != inputStream) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (connection != null) { + connection.disconnect();// 鍏抽棴杩滅▼杩炴帴 + } + } + return response; } - - /** - * - * @param datetype 闇�瑕佽В鏋愮殑鏃ユ湡鐨勬牸寮忓锛�"yyyy-MM-dd HH:mm:ss" - * @return 寰楀埌瀵瑰簲鐨刧son瀵硅薄 - */ - public static Gson getGson(String datetype){ - return new GsonBuilder().setDateFormat(datetype).create(); + public static Response<String> doPost(String httpUrl, String requestBody) { + HttpURLConnection connection = null; + InputStream inputStream = null; + OutputStream outputStream =null; + BufferedReader bufferedReader = null; + String result = null;// 杩斿洖缁撴灉瀛楃涓� + Response<String> response = new Response<>(); + try { + // 鍒涘缓杩滅▼url杩炴帴瀵硅薄 + URL url = new URL(new StringBuffer(httpUrl).toString()); + // 閫氳繃杩滅▼url杩炴帴瀵硅薄鎵撳紑涓�涓繛鎺ワ紝寮鸿浆鎴恏ttpURLConnection绫� + connection = (HttpURLConnection) url.openConnection(); + // 璁剧疆杩炴帴鏂瑰紡锛歱ost + connection.setRequestMethod("POST"); + // 璁剧疆杩炴帴涓绘満鏈嶅姟鍣ㄧ殑瓒呮椂鏃堕棿锛�15000姣 + connection.setConnectTimeout(5000); + // 璁剧疆璇诲彇杩滅▼杩斿洖鐨勬暟鎹椂闂达細60000姣 + connection.setReadTimeout(6000); + //璁剧疆contentType鍥哄畾涓篴pplication/json + connection.setRequestProperty("Content-Type","application/json"); + //璇锋眰浣� + connection.setDoOutput(true); + outputStream = connection.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(outputStream,"UTF-8"); + + if(requestBody != null) { + osw.write(requestBody); + osw.flush(); + } + // 鍙戦�佽姹� + connection.connect(); + // 閫氳繃connection杩炴帴锛岃幏鍙栬緭鍏ユ祦 + if (connection.getResponseCode() == 200) { + inputStream = connection.getInputStream(); + // 灏佽杈撳叆娴侊紝骞舵寚瀹氬瓧绗﹂泦 + bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); + // 瀛樻斁鏁版嵁 + StringBuilder sbf = new StringBuilder(); + String temp; + while ((temp = bufferedReader.readLine()) != null) { + sbf.append(temp); + sbf.append(System.getProperty("line.separator")); + } + result = sbf.toString(); + response.set(1); + response.setData(result); + } + } catch (IOException e) { + String message = e.getMessage(); + response.set(0); + response.setMsg(message); + } finally { + // 鍏抽棴璧勬簮 + if (null != bufferedReader) { + try { + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (null != inputStream) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (connection != null) { + connection.disconnect();// 鍏抽棴杩滅▼杩炴帴 + } + if (null != outputStream) { + try { + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return response; } - + /** - * 鑾峰彇榛樿鐨刧son瀵硅薄 + * 灏唌ap鍨嬭浆涓鸿姹傚弬鏁板瀷 + * + * @param data * @return */ - public static Gson getGson(){ - return new Gson(); - } - - - public static <T> T getObject(String jsonstring,Type listtype){ - Gson gson=new Gson(); - T t=null; - try { - t=gson.fromJson(jsonstring, listtype); - } catch (JsonSyntaxException e) { - e.printStackTrace(); + public static String urlEncode(Map<String, ?> data) { + StringBuilder sb = new StringBuilder(); + for (Map.Entry<String, ?> i : data.entrySet()) { + try { + sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } } - return t; + String result = sb.toString(); + result = result.substring(0, result.lastIndexOf("&")); + return result; } + /** + * 鐢ㄤ簬Video 鐨凥ttp鎺ュ彛璇锋眰 + * 鎵цGET璇锋眰,浼犲叆Map鍙傛暟鍜孶rl,鑾峰彇鍝嶅簲 + * 杩欎釜瀵瑰師濮嬬殑doGet鏂规硶杩涜浜嗚皟鐢�,瀵圭粨鏋滆繘琛屽悗缁�昏緫鍐嶅鐞�,灞炰簬瀹氬埗鍖栫殑鎺ュ彛浜�. + */ + public static Response4Http doGet(String httpUrl, Map<String, ?> params) { + Response4Http responseResult = new Response4Http<>(); + + String queryParams = urlEncode(params); + Response response = HttpUtil.doGet(httpUrl, queryParams); + Integer httpResponseCode = response.getCode(); + String responseJson = (String) response.getData(); + if(httpResponseCode == 1) { //璇锋眰鎴愬姛 + Response4Http responseHttp = JsonUtil.getGson().fromJson(responseJson, Response4Http.class); + if(responseHttp.getCode() == 0 ) { //鎺ュ彛杩斿洖鐘舵�佺爜涓烘垚鍔� + //杩斿洖淇℃伅鏈� data灞炴�� + if(responseHttp.getData()!=null) { + return responseResult.setII(1,true,responseHttp.getData(),null); + } + //杩斿洖淇℃伅鏈� status灞炴�� + else if(responseHttp.getStatus() !=null){ + return responseResult.setStatus(1,true,responseHttp.getStatus()); + } + //杩斿洖淇℃伅鏈� result灞炴�� + else if(responseHttp.getResult() !=null){ + responseResult.setResult(responseHttp.getResult()); + return responseResult.set(1,true); + } + else{ + return responseResult.set(1,true,"璇锋眰鎴愬姛涓旇繑鍥炴暟鎹甯�,鏃犳暟鎹俊鎭�"); + } + }else{ //鎺ュ彛杩斿洖鐘舵�佺爜涓哄け璐� + // 杩斿洖淇℃伅鏈� msg灞炴�� + return responseResult.set(1,false,responseHttp.getMsg()); + } + }else{ //璇锋眰澶辫触 + return responseResult.set(0,response.getData(),response.getMsg()); + } + + } } -- Gitblit v1.9.1