| | |
| | | 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.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.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")); |
| | | |
| | | // 非空 |
| | | 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; |
| | | } |
| | | |
| | | private static final String BOUNDARY = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; |
| | | private static final String LINE_FEED = "\r\n"; |
| | |
| | | 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 |
| | |
| | | return responseBody; |
| | | } |
| | | |
| | | /** |
| | | * 上传同步2 |
| | | * @param url |
| | | * @param entity |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static String sendPostRequestTwo(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()); |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 删除同步 |
| | |
| | | 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(); |
| | | } |
| | | |
| | | } |
| | | |