瀏覽代碼

添加上传接口

gaoxiong 2 年之前
父節點
當前提交
c64239d484

+ 26 - 3
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/controller/SysFileController.java

@@ -19,7 +19,7 @@ import javax.servlet.http.HttpServletResponse;
 
 /**
  * 文件请求处理
- * 
+ *
  * @author xunmei
  */
 @RestController
@@ -52,7 +52,7 @@ public class SysFileController
             String url = sysFileService.uploadFile(file);
             SysFile sysFile = new SysFile();
             sysFile.setName(FileUtils.getName(url));
-            sysFile.setUrl(null);
+            sysFile.setUrl(url);
             String code = DesUtil.encrypt(url, secretKey);
             sysFile.setCode(code);
             return R.ok(sysFile);
@@ -64,6 +64,29 @@ public class SysFileController
         }
     }
 
+    @PostMapping("/uploadFile")
+    public R<SysFile> uploadFile(MultipartFile file,String busType)
+    {
+        try
+        {
+            // 上传并返回访问地址
+            String url = sysFileService.uploadFile(file,busType);
+            SysFile sysFile = new SysFile();
+            sysFile.setName(FileUtils.getName(url));
+            sysFile.setUrl(url);
+            String code = DesUtil.encrypt(url, secretKey);
+            sysFile.setCode(code);
+            return R.ok(sysFile);
+        }
+        catch (Exception e)
+        {
+            log.error("上传文件失败", e);
+            return R.fail(e.getMessage());
+        }
+    }
+
+
+
     @PostMapping("/downloadFile")
     public void downloadFile(HttpServletResponse response,@RequestBody SysFile sysFile){
         try {
@@ -76,4 +99,4 @@ public class SysFileController
 
 
     }
-}
+}

+ 12 - 2
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/service/ISysFileService.java

@@ -7,20 +7,30 @@ import java.io.ByteArrayOutputStream;
 
 /**
  * 文件上传接口
- * 
+ *
  * @author xunmei
  */
 public interface ISysFileService
 {
     /**
      * 文件上传接口
-     * 
+     *
      * @param file 上传的文件
      * @return 访问地址
      * @throws Exception
      */
      String uploadFile(MultipartFile file) throws Exception;
 
+
+    /**
+     * 文件上传接口
+     *
+     * @param file 上传的文件
+     * @return 访问地址
+     * @throws Exception
+     */
+    String uploadFile(MultipartFile file,String busType) throws Exception;
+
     /**
      * 文件下载接口
      * @param response

+ 10 - 2
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/service/LocalSysFileServiceImpl.java

@@ -15,13 +15,14 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 import java.net.URLDecoder;
 import java.nio.charset.StandardCharsets;
 
 /**
  * 本地文件存储
- * 
+ *
  * @author xunmei
  */
 @Primary
@@ -42,7 +43,7 @@ public class LocalSysFileServiceImpl implements ISysFileService
 
     /**
      * 本地文件上传接口
-     * 
+     *
      * @param file 上传的文件
      * @return 访问地址
      * @throws Exception
@@ -56,6 +57,13 @@ public class LocalSysFileServiceImpl implements ISysFileService
     }
 
     @Override
+    public String uploadFile(MultipartFile file, String busType) throws Exception {
+        String name = FileUploadUtils.upload(localFilePath, file,busType);
+//        String url = domain + localFilePrefix + name;
+        return name;
+    }
+
+    @Override
     public void downloadFile(HttpServletResponse response, String filePath){
         ByteArrayOutputStream out = null;
         try {

+ 19 - 2
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/utils/FileUploadUtils.java

@@ -19,7 +19,7 @@ import java.util.Objects;
 
 /**
  * 文件上传工具类
- * 
+ *
  * @author xunmei
  */
 public class FileUploadUtils
@@ -58,6 +58,23 @@ public class FileUploadUtils
         }
     }
 
+    public static final String upload(String baseDir, MultipartFile file,String busType) throws IOException
+    {
+        try
+        {
+            String upload = upload(baseDir + File.separator + busType, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
+            return File.separator + busType + upload;
+        }
+        catch (FileException fe)
+        {
+            throw new IOException(fe.getDefaultMessage(), fe);
+        }
+        catch (Exception e)
+        {
+            throw new IOException(e.getMessage(), e);
+        }
+    }
+
     /**
      * 文件上传
      *
@@ -183,4 +200,4 @@ public class FileUploadUtils
         }
         return false;
     }
-}
+}