Commit f11bb30d08425821cd65cc4ae56acf5ee28c0a5b
1 parent
57b3d3a3
Exists in
master
优化
Showing
2 changed files
with
24 additions
and
1 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilHttpByOkHttp.java
| ... | ... | @@ -185,6 +185,29 @@ public class UtilHttpByOkHttp { |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | |
| 188 | + | |
| 189 | + public static ResponseBody sendPostFile(String url, File file,String fileKey,Map<String, String> header) throws Exception { | |
| 190 | + OkHttpClient client = new OkHttpClient(); | |
| 191 | + RequestBody requestBody = new MultipartBody.Builder() | |
| 192 | + .setType(MultipartBody.FORM) | |
| 193 | + .addFormDataPart(fileKey, file.getName(), | |
| 194 | + RequestBody.create(MediaType.parse("multipart/form-data"), file)) | |
| 195 | + .build(); | |
| 196 | + | |
| 197 | + Request request = new Request.Builder() | |
| 198 | + .headers(Headers.of(header)) | |
| 199 | + .url(url) | |
| 200 | + .post(requestBody) | |
| 201 | + .build(); | |
| 202 | + | |
| 203 | + Response response = client.newCall(request).execute(); | |
| 204 | + if (!response.isSuccessful()){ | |
| 205 | + throw new IOException("Unexpected code " + response); | |
| 206 | + } | |
| 207 | + return response.body(); | |
| 208 | + } | |
| 209 | + | |
| 210 | + | |
| 188 | 211 | static TrustManager[] trustAllCerts = new TrustManager[]{ |
| 189 | 212 | new X509TrustManager() { |
| 190 | 213 | public java.security.cert.X509Certificate[] getAcceptedIssuers() { | ... | ... |