qirong
2023-12-22 ac2873bd37eeb496b0e9dd62d66e9fc4b38ef39b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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 {
 
    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;
    }
 
    /**
     * 上传同步 视频专网 -- 公安内网
     * @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();
    }
 
}