|
@@ -3,6 +3,7 @@ package com.qmth.jkserver.util;
|
|
|
import com.qmth.jkserver.constant.SystemConstant;
|
|
|
import com.qmth.jkserver.core.exception.JkServerException;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.http.Consts;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
@@ -22,10 +23,11 @@ import org.apache.http.message.BasicHeader;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.protocol.HTTP;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
-import org.apache.tomcat.util.http.fileupload.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
@@ -260,13 +262,13 @@ public class HttpUtil {
|
|
|
post.setConfig(requestConfig);
|
|
|
post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
|
|
|
post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
|
|
|
- post.setHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE + "; charset=" + SystemConstant.CHARSET_NAME);
|
|
|
- post.setHeader("Accept", SystemConstant.CONTENT_TYPE);
|
|
|
+ post.setHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE_JSON + "; charset=" + SystemConstant.CHARSET_NAME);
|
|
|
+ post.setHeader("Accept", SystemConstant.CONTENT_TYPE_JSON);
|
|
|
|
|
|
String encoderJson = URLEncoder.encode(json, SystemConstant.CHARSET_NAME);
|
|
|
StringEntity se = new StringEntity(encoderJson);
|
|
|
se.setContentType("text/json");
|
|
|
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE));
|
|
|
+ se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE_JSON));
|
|
|
post.setEntity(se);
|
|
|
// 执行请求,获取响应
|
|
|
return getRespString(post);
|
|
@@ -297,9 +299,62 @@ public class HttpUtil {
|
|
|
// StringEntity se = new StringEntity(encoderJson);
|
|
|
StringEntity se = new StringEntity(json);
|
|
|
se.setContentType("text/json");
|
|
|
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE));
|
|
|
+ se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, SystemConstant.CONTENT_TYPE_JSON));
|
|
|
post.setEntity(se);
|
|
|
// 执行请求,获取响应
|
|
|
return getRespString(post);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post file
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param params
|
|
|
+ * @param headers
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String postFile(String url, Map<String, Object> params, Map<String, String> headers) throws IOException {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(SystemConstant.CONNECT_TIME_OUT)// 连接主机服务超时时间
|
|
|
+ .setConnectionRequestTimeout(SystemConstant.CONNECT_TIME_OUT)// 请求超时时间
|
|
|
+ .setSocketTimeout(SystemConstant.SOCKET_CONNECT_TIME_OUT)// 数据读取超时时间
|
|
|
+ .build();
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
+ for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ post.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ File file = null;
|
|
|
+ try {
|
|
|
+ MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
|
|
|
+ // 解决中文文件名乱码问题
|
|
|
+ entityBuilder.setMode(HttpMultipartMode.RFC6532);
|
|
|
+ entityBuilder.setCharset(Consts.UTF_8);
|
|
|
+ ContentType contentType = ContentType.create(ContentType.MULTIPART_FORM_DATA.getMimeType(), Consts.UTF_8);
|
|
|
+ if (!CollectionUtils.isEmpty(params)) {
|
|
|
+ for (String key : params.keySet()) {
|
|
|
+ if (key.contains("files")) {
|
|
|
+ MultipartFile multipartFile = (MultipartFile) params.get(key);
|
|
|
+ String[] strs = StringUtils.split(multipartFile.getOriginalFilename(), ".");
|
|
|
+ file = SystemConstant.getFileTempVar("." + strs[1]);
|
|
|
+ FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
|
|
|
+ entityBuilder.addBinaryBody(key, file, ContentType.DEFAULT_BINARY, file.getName());
|
|
|
+ } else {
|
|
|
+ entityBuilder.addPart(key, new StringBody(String.valueOf(params.get(key)), contentType));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ entityBuilder.setContentType(ContentType.MULTIPART_FORM_DATA);
|
|
|
+ post.setEntity(entityBuilder.build());
|
|
|
+ post.setConfig(requestConfig);
|
|
|
+ return getRespString(post);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (Objects.nonNull(file)) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|