Browse Source

本地代码优化提交

高家顺 4 years ago
parent
commit
834b8a5246

+ 10 - 0
src/main/java/com/macro/mall/tiny/common/util/UploadUtils.java

@@ -76,6 +76,16 @@ public class UploadUtils {
         }
     }
 
+    public static void putChunkMap(String key, int chunks) {
+        if (!isExist(key)) {
+            synchronized (UploadUtils.class) {
+                if (!isExist(key)) {
+                    chunkMap.put(key, new Value(chunks));
+                }
+            }
+        }
+    }
+
     /**
      * 获取随机生成的文件名
      *

+ 6 - 0
src/main/java/com/macro/mall/tiny/config/UploadConfig.java

@@ -8,6 +8,7 @@ public class UploadConfig {
 
     public static String path;
     public static String tempPath;
+    public static String powerPath;
 
     @Value("${upload.path}")
     public void setPath(String path) {
@@ -18,4 +19,9 @@ public class UploadConfig {
     public void setTempPath(String path) {
         UploadConfig.tempPath = path;
     }
+
+    @Value("${upload.power-path}")
+    public void setPowerPath(String powerPath) {
+        UploadConfig.powerPath = powerPath;
+    }
 }

+ 0 - 46
src/main/java/com/macro/mall/tiny/modules/business/controller/BigFileUploadController.java

@@ -1,46 +0,0 @@
-package com.macro.mall.tiny.modules.business.controller;
-
-import com.macro.mall.tiny.modules.business.service.FileService;
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiOperation;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.IOException;
-
-/**
- * 大文件上传
- */
-@RestController
-@RequestMapping("/file")
-@CrossOrigin
-@Api(tags = "文件上传相关接口", description = "提供文件上传相关API")
-public class BigFileUploadController {
-    @Autowired
-    private FileService fileService;
-
-
-    /**
-     * @param size
-     * @param chunks 文件分块数
-     * @param chunk  文件分块序号
-     * @param file   文件
-     * @throws IOException
-     */
-    @ApiOperation("文件上传接口")
-    @PostMapping("/upload")
-    public void upload(Long size,
-                       Integer chunks,
-                       Integer chunk,
-                       MultipartFile file) throws IOException {
-        if (chunks != null && chunks != 0) {
-            fileService.uploadWithBlock(size, chunks, chunk, file);
-        } else {
-            fileService.upload(file);
-        }
-    }
-}

+ 94 - 0
src/main/java/com/macro/mall/tiny/modules/business/controller/FileController.java

@@ -0,0 +1,94 @@
+package com.macro.mall.tiny.modules.business.controller;
+
+import com.macro.mall.tiny.common.api.CommonResult;
+import com.macro.mall.tiny.config.UploadConfig;
+import com.macro.mall.tiny.modules.business.service.FileService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.tomcat.util.http.fileupload.IOUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * 大文件上传
+ */
+@Slf4j
+@RestController
+@RequestMapping("/file")
+@CrossOrigin
+@Api(tags = "FileController", description = "提供文件上传相关API")
+public class FileController {
+    @Autowired
+    private FileService fileService;
+
+
+    /**
+     * @param fileUploadParam
+     * @return void
+     * @describe 文件上传
+     * @author gjs
+     * @date 2021/3/25
+     * @since J7.23.0
+     */
+    @ApiOperation("文件上传接口")
+    @PostMapping("/upload")
+    public void upload(String name,
+                       Long size,
+                       Integer chunks,
+                       Integer chunk,
+                       MultipartFile file) throws IOException {
+        if (chunks != null && chunks != 0) {
+            fileService.uploadWithBlock(name, size, chunks, chunk, file);
+        } else {
+            fileService.upload(file);
+        }
+    }
+
+    @ApiOperation("文件下载接口")
+    @PostMapping("/download")
+    public Object downloadFile(@RequestParam String fileName, final HttpServletResponse response) {
+        OutputStream os = null;
+        InputStream is = null;
+        try {
+            // 取得输出流
+            os = response.getOutputStream();
+            // 清空输出流
+            response.reset();
+            response.setContentType("application/x-download;charset=UTF-8");
+            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
+//            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
+            //读取流
+            File f = new File(UploadConfig.path + fileName);
+            is = new FileInputStream(f);
+            //复制
+            IOUtils.copy(is, response.getOutputStream());
+            response.getOutputStream().flush();
+        } catch (IOException e) {
+            return CommonResult.failed("下载附件失败,error:" + e.getMessage());
+        }
+        //文件的关闭放在finally中
+        finally {
+            try {
+                if (is != null) {
+                    is.close();
+                }
+            } catch (IOException e) {
+                log.error("关闭文件输入流失败:", e);
+            }
+            try {
+                if (os != null) {
+                    os.close();
+                }
+            } catch (IOException e) {
+                log.error("关闭文件输出流失败:", e);
+            }
+        }
+        return null;
+    }
+}

+ 31 - 0
src/main/java/com/macro/mall/tiny/modules/business/dto/FileUploadParam.java

@@ -0,0 +1,31 @@
+package com.macro.mall.tiny.modules.business.dto;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author gjs
+ * @description
+ * @date 2021/3/25 10:17
+ */
+@Getter
+@Setter
+public class FileUploadParam {
+
+    @ApiModelProperty(value = "原始文件总大小(Byte)")
+    private Long size;
+
+    @ApiModelProperty(value = "分块总数")
+    private Integer chunks;
+
+    @ApiModelProperty(value = "当前分块数(0~n-1)")
+    private Integer chunk;
+
+    @NotNull
+    @ApiModelProperty(value = "当前分块的文件", required = true)
+    private MultipartFile file;
+}

+ 1 - 1
src/main/java/com/macro/mall/tiny/modules/business/service/FileService.java

@@ -13,5 +13,5 @@ public interface FileService {
 
     void upload(MultipartFile file) throws IOException;
 
-    void uploadWithBlock(Long size, Integer chunks, Integer chunk, MultipartFile file) throws IOException;
+    void uploadWithBlock(String name, Long size, Integer chunks, Integer chunk, MultipartFile file) throws IOException;
 }

+ 7 - 6
src/main/java/com/macro/mall/tiny/modules/business/service/impl/FileServiceImpl.java

@@ -38,15 +38,16 @@ public class FileServiceImpl implements FileService {
      * @param file
      * @throws IOException
      */
-    public void uploadWithBlock(Long size,
+    public void uploadWithBlock(String name,
+                                Long size,
                                 Integer chunks,
                                 Integer chunk,
                                 MultipartFile file) throws IOException {
-        String fileName = file.getOriginalFilename();
-        FileUtils.writeWithBlok(UploadConfig.tempPath + fileName, size, file.getInputStream(), file.getSize(), chunks, chunk);
-        addChunk(fileName, chunk);
-        if (isUploaded(fileName)) {
-            removeKey(fileName);
+        putChunkMap(name, chunks);
+        FileUtils.writeWithBlok(UploadConfig.tempPath + name, size, file.getInputStream(), file.getSize(), chunks, chunk);
+        addChunk(name, chunk);
+        if (isUploaded(name)) {
+            removeKey(name);
         }
     }
 }

+ 2 - 1
src/main/resources/application.yml

@@ -57,4 +57,5 @@ secure:
 
 upload:
   temp-path: ./temp/
-  path: ./upload/ #文件上传路径
+  path: ./upload/ #文件上传路径
+  power-path: ./power/