From af395123e74c61c8aa33bc7d2c48a3528d65f887 Mon Sep 17 00:00:00 2001 From: liusheng <337615773@qq.com> Date: 星期三, 16 八月 2023 19:57:51 +0800 Subject: [PATCH] 代码提交 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientKit.java | 75 +++++++++++++++++++++---------------- 1 files changed, 42 insertions(+), 33 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientKit.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientKit.java index c7ffa9f..040f651 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientKit.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientKit.java @@ -37,8 +37,7 @@ try { StringEntity s = new StringEntity(json.toString(), "utf-8"); - s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, - "application/json")); + s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(s); // 鍙戦�佽姹� @@ -46,12 +45,10 @@ // 鑾峰彇鍝嶅簲杈撳叆娴� InputStream inStream = httpResponse.getEntity().getContent(); - BufferedReader reader = new BufferedReader(new InputStreamReader( - inStream, "utf-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8")); StringBuilder strber = new StringBuilder(); String line = null; - while ((line = reader.readLine()) != null) - strber.append(line + "\n"); + while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); result = strber.toString(); @@ -65,8 +62,7 @@ } catch (Exception e) { System.out.println("璇锋眰寮傚父"); throw new RuntimeException(e); - } - finally { + } finally { //client. } return result; @@ -74,11 +70,11 @@ public static String sendPostWithFile(File lrcFile, String urlStr) throws Exception { String BOUNDARY = java.util.UUID.randomUUID().toString(); - MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.defaultCharset()); + MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.forName("UTF-8")); multipartEntity.addPart("lyricsUpload", new FileBody(lrcFile)); //lyricsUpload鏂囦欢鍚� HttpPost request = new HttpPost(urlStr); //杩滅▼鎺ュ彛 request.setEntity(multipartEntity); - request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY); + request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY + ";charset=UTF-8"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(request); @@ -89,32 +85,49 @@ while ((line = in.readLine()) != null) { buffer.append(line); } - //Flogger.info("鍙戦�佹秷鎭敹鍒扮殑杩斿洖锛�" + buffer.toString()); - String remark = ""; - return buffer.toString(); - // org.json.JSONObject JSONObject = new org.json.JSONObject(buffer.toString()); + return new String(buffer.toString().getBytes(), "UTF-8"); } - public static String postOpr(String url,String json) - { + + // public static String sendPostWithFile(File lrcFile, String urlStr) throws Exception { +// String BOUNDARY = java.util.UUID.randomUUID().toString(); +// MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.defaultCharset()); +// multipartEntity.addPart("lyricsUpload", new FileBody(lrcFile)); //lyricsUpload鏂囦欢鍚� +// HttpPost request = new HttpPost(urlStr); //杩滅▼鎺ュ彛 +// request.setEntity(multipartEntity); +// request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY); +// +// DefaultHttpClient httpClient = new DefaultHttpClient(); +// HttpResponse response = httpClient.execute(request); +// InputStream is = response.getEntity().getContent(); +// BufferedReader in = new BufferedReader(new InputStreamReader(is)); +// StringBuffer buffer = new StringBuffer(); +// String line = ""; +// while ((line = in.readLine()) != null) { +// buffer.append(line); +// } +// //Flogger.info("鍙戦�佹秷鎭敹鍒扮殑杩斿洖锛�" + buffer.toString()); +// String remark = ""; +// return buffer.toString(); +// // org.json.JSONObject JSONObject = new org.json.JSONObject(buffer.toString()); +// } + public static String postOpr(String url, String json) { //HttpClient httpClient = new HttpClient(); org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(); PostMethod httpPost = new PostMethod(url); - httpPost.setRequestHeader("Content-Type","application/json;charset=utf-8"); + httpPost.setRequestHeader("Content-Type", "application/json;charset=utf-8"); - try - { - RequestEntity entity = new StringRequestEntity(json,"Content-Type","UTF-8"); + try { + RequestEntity entity = new StringRequestEntity(json, "Content-Type", "UTF-8"); httpPost.setRequestEntity(entity); httpClient.executeMethod(httpPost); - return httpPost.getResponseBodyAsString(); - } - catch (Exception e) - { + return httpPost.getResponseBodyAsString(); + } catch (Exception e) { e.printStackTrace(); } - return ""; + return ""; } + public static String postMsg(String url, JSONObject json) { CloseableHttpClient client = HttpClients.createDefault(); @@ -129,8 +142,7 @@ try { StringEntity s = new StringEntity(json.toJSONString(), "utf-8"); - s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, - "application/json")); + s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(s); // 鍙戦�佽姹� @@ -138,12 +150,10 @@ // 鑾峰彇鍝嶅簲杈撳叆娴� InputStream inStream = httpResponse.getEntity().getContent(); - BufferedReader reader = new BufferedReader(new InputStreamReader( - inStream, "utf-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8")); StringBuilder strber = new StringBuilder(); String line = null; - while ((line = reader.readLine()) != null) - strber.append(line + "\n"); + while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); result = strber.toString(); @@ -157,8 +167,7 @@ } catch (Exception e) { System.out.println("璇锋眰寮傚父"); throw new RuntimeException(e); - } - finally { + } finally { //client. } return result; -- Gitblit v1.9.3