Эх сурвалжийг харах

增加retrofit方式进行文件上传的例子

luoshi 3 жил өмнө
parent
commit
8f772d1cd9

+ 19 - 0
api-demo/src/main/java/com/qmth/demo/api/client/FileTestClient.java

@@ -0,0 +1,19 @@
+package com.qmth.demo.api.client;
+
+import com.qmth.boot.core.retrofit.annotatioin.RetrofitClient;
+import okhttp3.MultipartBody;
+import retrofit2.http.Multipart;
+import retrofit2.http.POST;
+import retrofit2.http.Part;
+import retrofit2.http.Query;
+
+@RetrofitClient(baseUrl = "http://127.0.01:8080/api/file/")
+public interface FileTestClient {
+
+    @POST("upload")
+    @Multipart
+    String upload(@Query("path") String path, @Query("md5") String md5, @Part MultipartBody.Part file);
+
+    @POST("server")
+    String getServer();
+}

+ 29 - 1
api-demo/src/main/java/com/qmth/demo/api/controller/FileController.java

@@ -4,15 +4,21 @@ import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.core.fss.store.FileStore;
+import com.qmth.boot.core.retrofit.utils.UploadFile;
+import com.qmth.boot.tools.models.ByteArray;
+import com.qmth.demo.api.client.FileTestClient;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.File;
+import java.io.IOException;
 
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/file")
-@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
+@Aac(auth = BOOL.FALSE)
 public class FileController {
 
     //    @Resource
@@ -21,6 +27,9 @@ public class FileController {
     @Resource
     FileStore fileStore;
 
+    @Resource
+    FileTestClient fileTestClient;
+
     @RequestMapping("/server")
     public Object server() {
         //return fileService.getFileStore("public").getServer();
@@ -32,4 +41,23 @@ public class FileController {
         //return fileService.getFileStore("public").exist(path);
         return fileStore.exist(path);
     }
+
+    @RequestMapping("/upload")
+    public Object upload(@RequestParam String path, @RequestParam String md5, @RequestParam MultipartFile file)
+            throws Exception {
+        fileStore.write(path, file.getInputStream(), md5);
+        return path;
+    }
+
+    @RequestMapping("/client/upload")
+    public Object clientUpload(@RequestParam String path, @RequestParam String name) throws Exception {
+        File file = new File(path);
+        String md5 = ByteArray.md5(file).toHexString();
+        return fileTestClient.upload(name, md5, UploadFile.build("file", name, file));
+    }
+
+    @RequestMapping("/client/server")
+    public Object clientServer() throws IOException {
+        return fileTestClient.getServer();
+    }
 }

+ 11 - 0
api-demo/src/main/java/com/qmth/demo/api/entity/TokenInfo.java

@@ -0,0 +1,11 @@
+package com.qmth.demo.api.entity;
+
+import com.qmth.boot.core.retrofit.utils.SignatureInfo;
+import com.qmth.boot.tools.signature.SignatureType;
+
+public class TokenInfo extends SignatureInfo {
+
+    public TokenInfo(String session, String token) {
+        super(SignatureType.TOKEN, session, token);
+    }
+}

+ 1 - 1
api-demo/src/main/resources/application.properties

@@ -2,7 +2,7 @@ server.port=8080
 
 #com.qmth.api.uri-prefix=/aaa
 com.qmth.api.metrics-endpoint=/metrics
-#com.qmth.api.global-auth=false
+com.qmth.api.global-auth=false
 #com.qmth.api.global-strict=false
 #com.qmth.api.global-rate-limit=1/5s