From 762167f3b4248055fc020277af1bd5724e29fec0 Mon Sep 17 00:00:00 2001 From: qirong <2032486488@qq.com> Date: 星期三, 22 十一月 2023 17:16:27 +0800 Subject: [PATCH] 上传同步 --- ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/uitil/HttpUtils.java | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 138 insertions(+), 0 deletions(-) diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/uitil/HttpUtils.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/uitil/HttpUtils.java index d3fb7aa..194dd02 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/uitil/HttpUtils.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/uitil/HttpUtils.java @@ -4,7 +4,20 @@ import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.dromara.common.oss.entity.SynchronousRequest; +import org.springframework.web.multipart.MultipartFile; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.HashMap; public class HttpUtils { @@ -38,5 +51,130 @@ } return jsonResult; } + + private static final String BOUNDARY = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; + private static final String LINE_FEED = "\r\n"; + private static final String CHARSET = "UTF-8"; + + public static File convert(MultipartFile multipartFile) throws IOException { + File file = new File(multipartFile.getOriginalFilename()); + multipartFile.transferTo(file); + return file; + } + + public static void sendPostRequest(String url, String stringParam, File fileParam) throws Exception { + + URL obj = new URL(url); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + + // 璁剧疆POST璇锋眰 + con.setRequestMethod("POST"); + con.setDoOutput(true); + con.setDoInput(true); + con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); + + // 鏋勫缓璇锋眰浣� + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); + wr.writeBytes("--" + BOUNDARY + LINE_FEED); + wr.writeBytes("Content-Disposition: form-data; name=\"stringParam\"" + LINE_FEED); + wr.writeBytes(LINE_FEED); + wr.writeBytes(stringParam + LINE_FEED); + + wr.writeBytes("--" + BOUNDARY + LINE_FEED); + wr.writeBytes("Content-Disposition: form-data; name=\"fileParam\"; filename=\"" + fileParam.getName() + "\"" + LINE_FEED); + wr.writeBytes("Content-Type: " + HttpURLConnection.guessContentTypeFromName(fileParam.getName()) + LINE_FEED); + wr.writeBytes("Content-Transfer-Encoding: binary" + LINE_FEED); + wr.writeBytes(LINE_FEED); + + FileInputStream inputStream = new FileInputStream(fileParam); + byte[] buffer = new byte[4096]; + int bytesRead = -1; + while ((bytesRead = inputStream.read(buffer)) != -1) { + wr.write(buffer, 0, bytesRead); + } + wr.writeBytes(LINE_FEED); + wr.writeBytes("--" + BOUNDARY + "--" + LINE_FEED); + + wr.flush(); + wr.close(); + + // 鍙戦�佽姹傚苟鑾峰彇鍝嶅簲 + int responseCode = con.getResponseCode(); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // 鎵撳嵃鍝嶅簲缁撴灉 + System.out.println("Response Code : " + responseCode); + System.out.println("Response : " + response.toString()); + } + + /** + * 涓婁紶鍚屾 + * @param url + * @param entity + * @return + * @throws IOException + */ + public static String sendPostRequest(String url, SynchronousRequest entity) throws IOException { + org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create().build(); + HttpPost httpPost = new HttpPost(url); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + + // 娣诲姞MultipartFile鍙傛暟 + builder.addBinaryBody("file", entity.getFile().getBytes(), ContentType.MULTIPART_FORM_DATA, entity.getFile().getOriginalFilename()); + + // 娣诲姞String鍙傛暟 + builder.addTextBody("path", entity.getPath(), ContentType.TEXT_PLAIN); + builder.addTextBody("ossId", entity.getOssId(), ContentType.TEXT_PLAIN); + builder.addTextBody("createBy", entity.getCreateBy(), ContentType.TEXT_PLAIN); + builder.addTextBody("password", entity.getPassword(), ContentType.TEXT_PLAIN); + + HttpEntity multipart = builder.build(); + httpPost.setEntity(multipart); + + HttpResponse response = httpClient.execute(httpPost); + String responseBody = EntityUtils.toString(response.getEntity()); + System.out.println("Response: " + responseBody); + return responseBody; + } + + /** + * 鍒犻櫎鍚屾 + * @param url + * @param parameter + * @throws Exception + */ + public static void sendDeleteRequest(String url, String parameter) throws Exception { + // 鎷兼帴URL鍜屽弬鏁� + String deleteUrl = url + "/" + parameter; + + // 鍒涘缓URL瀵硅薄鍜孒ttpURLConnection瀵硅薄 + URL obj = new URL(deleteUrl); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + + // 璁剧疆璇锋眰鏂规硶涓篋ELETE + con.setRequestMethod("DELETE"); + + // 鍙戦�佽姹傚苟鑾峰彇鍝嶅簲 + int responseCode = con.getResponseCode(); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // 鎵撳嵃鍝嶅簲缁撴灉 + System.out.println("Response Code : " + responseCode); + System.out.println("Response : " + response.toString()); + } } -- Gitblit v1.8.0