|
@@ -0,0 +1,159 @@
|
|
|
|
|
+package cn.com.ctop.job.bytedance.data.utils;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.google.gson.Gson;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import okhttp3.MediaType;
|
|
|
|
|
+import okhttp3.MultipartBody;
|
|
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
|
|
+import okhttp3.Request;
|
|
|
|
|
+import okhttp3.RequestBody;
|
|
|
|
|
+import okhttp3.Response;
|
|
|
|
|
+import okhttp3.ResponseBody;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.net.URLConnection;
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 文件上传
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class Okhttp3Utils {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private static Gson gson = new Gson();
|
|
|
|
|
+
|
|
|
|
|
+ public static JSONObject bytedanceGetRequest(String token, String url, Map<String, Object> paramsMap) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ StringBuilder postBody = null;
|
|
|
|
|
+ if (!Check.isNull(paramsMap)) {
|
|
|
|
|
+ postBody = new StringBuilder();
|
|
|
|
|
+ for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
|
|
|
|
|
+ if (entry.getValue() == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
|
|
|
|
|
+ "utf-8")).append("&");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!paramsMap.isEmpty()) {
|
|
|
|
|
+ postBody.deleteCharAt(postBody.length() - 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ String urlStr;
|
|
|
|
|
+ if (!Check.isNull(postBody)) {
|
|
|
|
|
+ urlStr = url + "?" + postBody;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ urlStr = url;
|
|
|
|
|
+ }
|
|
|
|
|
+ OkHttpClient client = TrustAllManager.filterSSL();
|
|
|
|
|
+ if (Check.isNull(client)) {
|
|
|
|
|
+ log.error("过滤SSL失败");
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(urlStr)
|
|
|
|
|
+ .method("GET", null)
|
|
|
|
|
+ .addHeader("Access-Token", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
|
|
+ if (response != null && response.code() == 200) {
|
|
|
|
|
+ ResponseBody body = response.body();
|
|
|
|
|
+ return JSONObject.parseObject(body.string());
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @Description: 实现服务间调用---文件上传
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject postUploadFile(String url, JSONObject params, String token) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 创建一个OkHttpClient,并设置sslSocketFactory
|
|
|
|
|
+ OkHttpClient client = TrustAllManager.filterSSL();
|
|
|
|
|
+ if (Check.isNull(client)) {
|
|
|
|
|
+ log.error("过滤SSL失败");
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ RequestBody requestBody = buildRequestBodyMultipart(params);
|
|
|
|
|
+
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(url)
|
|
|
|
|
+ .method("POST", requestBody)
|
|
|
|
|
+ .addHeader("Access-Token", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
|
|
+
|
|
|
|
|
+ // 响应内容数据解析
|
|
|
|
|
+ String responseBodyJson = "";
|
|
|
|
|
+ if (null != response && response.code() == 200) {
|
|
|
|
|
+ responseBodyJson = response.body().string();
|
|
|
|
|
+ return JSONObject.parseObject(responseBodyJson, JSONObject.class);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("文件上传失败!!!FailMsg:{}" + responseBodyJson);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static RequestBody buildRequestBodyMultipart(JSONObject params) throws Exception {
|
|
|
|
|
+ MultipartBody.Builder mpBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
|
|
|
|
|
+ for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
|
+ if (param.getValue() instanceof File) {
|
|
|
|
|
+ File file = (File) param.getValue();
|
|
|
|
|
+ addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
|
|
|
|
|
+ } else if (param.getValue() instanceof List) {
|
|
|
|
|
+ List list = (List) param.getValue();
|
|
|
|
|
+ for (Object item : list) {
|
|
|
|
|
+ if (item instanceof File) {
|
|
|
|
|
+ addPartToMultiPartBuilder(mpBuilder, param.getKey(), (File) item);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ addPartToMultiPartBuilder(mpBuilder, param.getKey(), param.getValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ addPartToMultiPartBuilder(mpBuilder, param.getKey(), param.getValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return mpBuilder.build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) throws Exception {
|
|
|
|
|
+ String fileName = file.getName();
|
|
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
|
|
+ byte[] byteFileContent = new byte[fileInputStream.available()];
|
|
|
|
|
+ fileInputStream.read(byteFileContent);
|
|
|
|
|
+ MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
|
|
|
|
|
+ mpBuilder.addFormDataPart(key, fileName, RequestBody.create(MediaType.parse("application/octet-stream"), byteFileContent));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, Object obj) {
|
|
|
|
|
+ RequestBody requestBody;
|
|
|
|
|
+ if (obj instanceof String) {
|
|
|
|
|
+ mpBuilder.addFormDataPart(key, (String) obj);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ mpBuilder.addFormDataPart(key, gson.toJson(obj));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public static String guessContentTypeFromFile(File file) {
|
|
|
|
|
+ String contentType = URLConnection.guessContentTypeFromName(file.getName());
|
|
|
|
|
+ if (contentType == null) {
|
|
|
|
|
+ return "application/octet-stream";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return contentType;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|