yanyi 4 سال پیش
والد
کامیت
947b2a5be0

+ 3 - 2
README.md

@@ -17,7 +17,7 @@ repositories {
 ~~~
 ### module 下添加
 ~~~
-implementation 'com.yanyi.benyanyi:okhttplib:2.0.6'
+implementation 'com.yanyi.benyanyi:okhttplib:2.0.7'
 ~~~
 
 或者
@@ -25,12 +25,13 @@ implementation 'com.yanyi.benyanyi:okhttplib:2.0.6'
 <dependency>
   <groupId>com.yanyi.benyanyi</groupId>
   <artifactId>okhttplib</artifactId>
-  <version>2.0.6</version>
+  <version>2.0.7</version>
   <type>aar</type>
 </dependency>
 ~~~
 
 ### 更新记录
+* 2021-03-13(2.0.7) 多文件上传
 * 2020-10-24(2.0.6) 修复缓存文件android6.0时磁盘未挂载时报错问题
 * 2020-09-25(2.0.5) 更改超时时间,添加下载文件覆盖
 * 2020-05-16(2.0.4) 修复上传大文件时OOM问题

+ 47 - 2
okhttplib/src/main/java/com/benyanyi/okhttp/type/HttpRequest.java

@@ -2,15 +2,16 @@ package com.benyanyi.okhttp.type;
 
 import android.content.Context;
 
-import com.benyanyi.okhttp.download.DownloadCall;
-import com.benyanyi.okhttp.download.DownloadConfig;
 import com.benyanyi.okhttp.call.HttpCall;
 import com.benyanyi.okhttp.call.RequestConfig;
+import com.benyanyi.okhttp.download.DownloadCall;
+import com.benyanyi.okhttp.download.DownloadConfig;
 import com.benyanyi.okhttp.util.FormatUtil;
 import com.benyanyi.okhttp.util.OkHttpLog;
 import com.google.gson.Gson;
 
 import java.io.File;
+import java.util.List;
 import java.util.Map;
 
 import okhttp3.FormBody;
@@ -160,6 +161,50 @@ public class HttpRequest implements RequestType {
         return send(true);
     }
 
+    @Override
+    public RequestConfig file(String... filePaths) {
+        MultipartBody.Builder builder = new MultipartBody.Builder()
+                .setType(MultipartBody.FORM);
+        if (filePaths != null && filePaths.length > 0) {
+            for (String str : filePaths) {
+                File file = new File(str);
+//                int indexOf = entry.getValue().toString().indexOf("/");
+//                RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
+                RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
+//                String fileName = entry.getKey().toString().substring(indexOf + 1);
+                builder.addFormDataPart("file", file.getName(), requestBody);
+            }
+        }
+        MultipartBody multipartBody = builder.build();
+        request = new Request.Builder()
+                .url(url)
+                .post(multipartBody)
+                .build();
+        return send(true);
+    }
+
+    @Override
+    public RequestConfig file(List<String> filePaths) {
+        MultipartBody.Builder builder = new MultipartBody.Builder()
+                .setType(MultipartBody.FORM);
+        if (filePaths != null && filePaths.size() > 0) {
+            for (String str : filePaths) {
+                File file = new File(str);
+//                int indexOf = entry.getValue().toString().indexOf("/");
+//                RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
+                RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
+//                String fileName = entry.getKey().toString().substring(indexOf + 1);
+                builder.addFormDataPart("file", file.getName(), requestBody);
+            }
+        }
+        MultipartBody multipartBody = builder.build();
+        request = new Request.Builder()
+                .url(url)
+                .post(multipartBody)
+                .build();
+        return send(true);
+    }
+
     @Override
     public DownloadConfig download() {
         return new DownloadCall.Builder().setContext(context).setUrl(url).setCover(false).setSuffix("").builder();

+ 6 - 1
okhttplib/src/main/java/com/benyanyi/okhttp/type/RequestType.java

@@ -1,8 +1,9 @@
 package com.benyanyi.okhttp.type;
 
-import com.benyanyi.okhttp.download.DownloadConfig;
 import com.benyanyi.okhttp.call.RequestConfig;
+import com.benyanyi.okhttp.download.DownloadConfig;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -25,6 +26,10 @@ public interface RequestType {
 
     RequestConfig file(Map<Object, Object> map);
 
+    RequestConfig file(String... filePaths);
+
+    RequestConfig file(List<String> filePaths);
+
     DownloadConfig download();
 
     /**