From ac2873bd37eeb496b0e9dd62d66e9fc4b38ef39b Mon Sep 17 00:00:00 2001
From: qirong <2032486488@qq.com>
Date: 星期五, 22 十二月 2023 13:38:26 +0800
Subject: [PATCH] ftp摆渡

---
 ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/uitil/HttpUtils.java |  198 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 171 insertions(+), 27 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..df3f77b 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
@@ -1,42 +1,186 @@
 package org.dromara.system.uitil;
 
+import cn.hutool.json.JSON;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.commons.io.IOUtils;
+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.dromara.common.oss.entity.SynchronousRequest2;
+import org.dromara.common.oss.entity.VideoRequest;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 
 public class HttpUtils {
-    public static String sendPostWithJson(String url, String jsonStr, HashMap<String,String> headers) {
-        // 杩斿洖鐨勭粨鏋�
-        String jsonResult = "";
-        try {
-            HttpClient client = new HttpClient();
-            // 杩炴帴瓒呮椂
-            client.getHttpConnectionManager().getParams().setConnectionTimeout(3*1000);
-            // 璇诲彇鏁版嵁瓒呮椂
-            client.getHttpConnectionManager().getParams().setSoTimeout(3*60*1000);
-            client.getParams().setContentCharset("UTF-8");
-            PostMethod postMethod = new PostMethod(url);
 
-            postMethod.setRequestHeader("content-type", headers.get("content-type"));
+    private static final String BOUNDARY = "----WebKitFormBoundary7MA4YWxkTrZu0gW";
+    private static final String LINE_FEED = "\r\n";
+    private static final String CHARSET = "UTF-8";
 
-            // 闈炵┖
-            if (null != jsonStr && !"".equals(jsonStr)) {
-                StringRequestEntity requestEntity = new StringRequestEntity(jsonStr, headers.get("content-type"), "UTF-8");
-                postMethod.setRequestEntity(requestEntity);
-            }
-            int status = client.executeMethod(postMethod);
-            if (status == HttpStatus.SC_OK) {
-                jsonResult = postMethod.getResponseBodyAsString();
-            } else {
-                throw new RuntimeException("鎺ュ彛杩炴帴澶辫触锛�");
-            }
-        } catch (Exception e) {
-            throw new RuntimeException("鎺ュ彛杩炴帴澶辫触锛�");
-        }
-        return jsonResult;
+    public static File convert(MultipartFile multipartFile) throws IOException {
+        File file = new File(multipartFile.getOriginalFilename());
+        multipartFile.transferTo(file);
+        return file;
     }
+
+    /**
+     * 涓婁紶鍚屾 瑙嗛涓撶綉 -- 鍏畨鍐呯綉
+     * @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);
+
+        ContentType contentType = ContentType.create("multipart/form-data", StandardCharsets.UTF_8);
+        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);
+        builder.addTextBody("fileName", entity.getFileName(), contentType);
+        builder.addTextBody("createTime", entity.getCreateTime(), 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 entity
+     * @return
+     * @throws IOException
+     */
+    public static String sendPostRequest2(String url, VideoRequest entity) throws IOException {
+        org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create().build();
+        HttpPost httpPost = new HttpPost(url);
+
+        ContentType contentType = ContentType.create("multipart/form-data", StandardCharsets.UTF_8);
+        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("fileName", entity.getFileName(), contentType);
+
+        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 String sendDeleteRequest(String url, String ossId) 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("ossId", ossId, 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 entity
+     * @return
+     * @throws IOException
+     */
+    public static String sendPostRequest2(String url, SynchronousRequest entity, byte[] bytes) throws IOException {
+        org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create().build();
+        HttpPost httpPost = new HttpPost(url);
+
+        ContentType contentType = ContentType.create("multipart/form-data", StandardCharsets.UTF_8);
+        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
+
+        // 娣诲姞MultipartFile鍙傛暟
+        builder.addBinaryBody("file", bytes, ContentType.MULTIPART_FORM_DATA, entity.getFileName());
+
+        // 娣诲姞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);
+        builder.addTextBody("fileName", entity.getFileName(), contentType);
+        builder.addTextBody("createTime", entity.getCreateTime(), 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 input 杈撳叆娴�
+     * @return byte[] 鏁扮粍
+     */
+    public static byte[] inputStream2byte(InputStream input) throws IOException {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        byte[] buffer = new byte[4096];
+        int n = 0;
+        while (-1 != (n = input.read(buffer))) {
+            output.write(buffer, 0, n);
+        }
+        return output.toByteArray();
+    }
+
 }
 

--
Gitblit v1.8.0