Prechádzať zdrojové kódy

Merge remote-tracking branch 'origin/dev-mysql' into dev-mysql

wangdy 8 rokov pred
rodič
commit
32797a3cf6

+ 15 - 1
src/main/java/com/uas/platform/b2c/common/base/service/impl/FileClientImpl.java

@@ -6,6 +6,7 @@ import com.uas.dfs.domain.MetaData;
 import com.uas.dfs.service.FileClient;
 import com.uas.platform.b2c.common.base.constant.FileClientConstant;
 import com.uas.platform.b2c.core.config.SysConf;
+import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ByteArrayResource;
 import org.springframework.http.*;
@@ -26,6 +27,8 @@ public class FileClientImpl implements FileClient {
 
     private final SysConf sysConf;
 
+    private final Logger logger = Logger.getLogger(getClass());
+
     @Autowired
     public FileClientImpl(RestTemplate restTemplate, SysConf sysConf) {
         this.restTemplate = restTemplate;
@@ -58,7 +61,18 @@ public class FileClientImpl implements FileClient {
         form.add("file", arrayResource);
 
         HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(form, headers);
-        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(sysConf.getUploadFileUrl() + FileClientConstant.FILE_UPLOAD, requestEntity, JSONObject.class);
+        ResponseEntity<JSONObject> responseEntity = null;
+        try {
+            logger.info("------------------");
+            logger.info(sysConf.getUploadFileUrl() + FileClientConstant.FILE_UPLOAD);
+            responseEntity = restTemplate.postForEntity(sysConf.getUploadFileUrl() + FileClientConstant.FILE_UPLOAD, requestEntity, JSONObject.class);
+            logger.info(responseEntity);
+            logger.info(responseEntity.getBody());
+            logger.info(responseEntity.getBody().getString("patch"));
+            logger.info("------------------");
+        }catch (Exception e) {
+            e.printStackTrace();
+        }
         return responseEntity.getBody().getString("path");
     }
 

+ 54 - 0
src/test/java/com/uas/platform/b2c/common/base/FileServiceTest.java

@@ -0,0 +1,54 @@
+package com.uas.platform.b2c.common.base;
+
+import com.uas.platform.b2c.BaseJunitTest;
+import com.uas.platform.b2c.common.base.service.FileService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.io.*;
+
+public class FileServiceTest extends BaseJunitTest {
+
+    @Autowired
+    private FileService fileService;
+
+    @Test
+    public void testUpload() {
+        String filePath = "C:\\Users\\Administrator\\Desktop\\kaifa.jpg";
+        File file = new File(filePath);
+        String fileName = file.getName();
+        byte[] bytes = file2byte(file);
+        String returnPath = fileService.save(fileName, bytes);
+        System.out.println(returnPath);
+    }
+
+    public static byte[] file2byte(String filePath) {
+        byte[] buffer = null;
+        File file = new File(filePath);
+        return file2byte(file);
+    }
+
+    public static byte[] file2byte(File file) {
+        byte[] buffer = null;
+        try {
+            FileInputStream fis = new FileInputStream(file);
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            byte[] b = new byte[1024];
+            int n;
+            while ((n = fis.read(b)) != -1) {
+                bos.write(b, 0, n);
+            }
+            fis.close();
+            bos.close();
+            buffer = bos.toByteArray();
+        }
+        catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+        }
+        return buffer;
+    }
+
+}