From f11bb30d08425821cd65cc4ae56acf5ee28c0a5b Mon Sep 17 00:00:00 2001 From: gaoming Date: Wed, 18 Sep 2019 21:24:52 +0800 Subject: [PATCH] 优化 --- build.gradle | 2 +- src/main/java/com/taover/util/UtilHttpByOkHttp.java | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 3d5529e..489f57a 100644 --- a/build.gradle +++ b/build.gradle @@ -54,7 +54,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '1.1.8' + version '1.1.9' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/util/UtilHttpByOkHttp.java b/src/main/java/com/taover/util/UtilHttpByOkHttp.java index 693f71d..6b41fd3 100644 --- a/src/main/java/com/taover/util/UtilHttpByOkHttp.java +++ b/src/main/java/com/taover/util/UtilHttpByOkHttp.java @@ -185,6 +185,29 @@ public class UtilHttpByOkHttp { } + + public static ResponseBody sendPostFile(String url, File file,String fileKey,Map header) throws Exception { + OkHttpClient client = new OkHttpClient(); + RequestBody requestBody = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart(fileKey, file.getName(), + RequestBody.create(MediaType.parse("multipart/form-data"), file)) + .build(); + + Request request = new Request.Builder() + .headers(Headers.of(header)) + .url(url) + .post(requestBody) + .build(); + + Response response = client.newCall(request).execute(); + if (!response.isSuccessful()){ + throw new IOException("Unexpected code " + response); + } + return response.body(); + } + + static TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { -- libgit2 0.21.2