|
|
@@ -4,24 +4,35 @@ import cn.seecoder.seecoderportalserver.service.HttpService;
|
|
|
import cn.seecoder.seecoderportalserver.web.rest.errors.CustomErrorException;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import org.apache.http.Consts;
|
|
|
-import org.apache.http.HttpEntity;
|
|
|
-import org.apache.http.NameValuePair;
|
|
|
+import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
+import org.apache.http.*;
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
-import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
-import org.apache.http.client.methods.HttpGet;
|
|
|
-import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.client.methods.*;
|
|
|
+import org.apache.http.conn.ClientConnectionManager;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.entity.mime.HttpMultipartMode;
|
|
|
+import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
+import org.apache.http.entity.mime.content.FileBody;
|
|
|
+import org.apache.http.entity.mime.content.StringBody;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.params.HttpParams;
|
|
|
+import org.apache.http.protocol.HTTP;
|
|
|
+import org.apache.http.protocol.HttpContext;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
@@ -42,6 +53,42 @@ public class HttpServiceImpl implements HttpService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String sendPostFile(String uri, MultipartFile file, JSONObject requestBody, String token) throws Exception {
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(uri); //创建http请求
|
|
|
+ if(token != null) {
|
|
|
+ httpPost.setHeader("Authorization", token); //设置请求头
|
|
|
+ }
|
|
|
+// httpPost.setConfig(RequestConfig.DEFAULT);
|
|
|
+
|
|
|
+// httpPost.setHeader("Content-Type", "multipart/form-data;chartset=UTF-8;boundary=----WebKitFormBoundaryAUx8rEOC9qObQ1XE");
|
|
|
+
|
|
|
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
+// builder.setCharset(Consts.UTF_8);
|
|
|
+// builder.setContentType(ContentType.create("multipart/form-data",Consts.UTF_8));
|
|
|
+ builder.setMode(HttpMultipartMode.RFC6532);
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+
|
|
|
+ System.out.println(JSON.toJSONString(requestBody));
|
|
|
+ StringBody contentBody = new StringBody(JSON.toJSONString(requestBody), ContentType.APPLICATION_JSON);
|
|
|
+ builder.addPart("meta", contentBody);
|
|
|
+// FileBody fb = new FileBody((File) file, ContentType.DEFAULT_BINARY);
|
|
|
+// builder.addPart("data", )
|
|
|
+ builder.addBinaryBody("data", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);
|
|
|
+// builder.addBinaryBody("data", file.getBytes());
|
|
|
+
|
|
|
+ HttpEntity multipartEntity = builder.build();
|
|
|
+ httpPost.setEntity(multipartEntity);
|
|
|
+
|
|
|
+ CloseableHttpResponse response = client.execute(httpPost);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String result = EntityUtils.toString(entity);
|
|
|
+ client.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
public String sendGet(String uri, List<NameValuePair> params, String token) throws Exception {
|
|
|
String completedUri;
|
|
|
if (params != null && !params.isEmpty()) {
|
|
|
@@ -63,6 +110,61 @@ public class HttpServiceImpl implements HttpService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String sendDelete(String uri, String jsonstr, String token) throws Exception {
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpDelete httpDelete = new HttpDelete(uri);
|
|
|
+ httpDelete.setConfig(RequestConfig.DEFAULT);
|
|
|
+ if(token != null) {
|
|
|
+ httpDelete.setHeader("Authorization", token);
|
|
|
+ }
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpDelete);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String result = EntityUtils.toString(entity);
|
|
|
+ httpClient.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String sendPut(String uri, JSONObject requestBody, String token) throws Exception {
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpPut httpPut = new HttpPut(uri);
|
|
|
+ if(token != null) {
|
|
|
+ httpPut.setHeader("Authorization", token);
|
|
|
+ }
|
|
|
+ httpPut.setHeader("Content-Type", "application/json;chartset=UTF-8");
|
|
|
+ StringEntity stringEntity = new StringEntity(requestBody.toJSONString());
|
|
|
+ httpPut.setEntity(stringEntity);
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpPut);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String result = EntityUtils.toString(entity);
|
|
|
+ httpClient.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String sendPutFile(String uri, MultipartFile file, String path, String token) throws Exception {
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpPut httpPut = new HttpPut(uri);
|
|
|
+ if(token != null) {
|
|
|
+ httpPut.setHeader("Authorization", token);
|
|
|
+ }
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+// httpPut.setHeader("Content-Type", "application/json;chartset=UTF-8");
|
|
|
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
+ builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, filename);
|
|
|
+ builder.addTextBody("path", path);
|
|
|
+ HttpEntity multipartEntity = builder.build();
|
|
|
+ httpPut.setEntity(multipartEntity);
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpPut);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String result = EntityUtils.toString(entity);
|
|
|
+ httpClient.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
public String getToken() {
|
|
|
String token;
|
|
|
HttpServletRequest httpServletRequest = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|