Browse Source

previewAndDownload

shiqian 4 năm trước cách đây
mục cha
commit
3c411a06f1

+ 10 - 0
boman-api/boman-domain/src/main/java/com.boman.domain/SysFile.java

@@ -27,6 +27,16 @@ public class SysFile
      */
     private String uid;
 
+    private String absolutePath;
+
+    public String getAbsolutePath() {
+        return absolutePath;
+    }
+
+    public void setAbsolutePath(String absolutePath) {
+        this.absolutePath = absolutePath;
+    }
+
     public String getName()
     {
         return name;

+ 18 - 0
boman-api/boman-domain/src/main/java/com.boman.domain/dto/FileDto.java

@@ -0,0 +1,18 @@
+package com.boman.domain.dto;
+
+
+import lombok.Data;
+
+/**
+ * @author shiqian
+ * @date 2021070811:09
+ **/
+@Data
+public class FileDto {
+
+
+    private String absolutePath;
+
+    private Boolean preview;
+
+}

+ 31 - 9
boman-modules/boman-file/src/main/java/com/boman/file/controller/SysFileController.java

@@ -1,18 +1,23 @@
 package com.boman.file.controller;
 
 import com.boman.domain.dto.AjaxResult;
+import com.boman.domain.dto.FileDto;
+import org.apache.commons.lang3.BooleanUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import com.boman.domain.dto.R;
 import com.boman.common.core.utils.file.FileUtils;
 import com.boman.file.service.ISysFileService;
 import com.boman.domain.SysFile;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URL;
+import java.util.List;
+
 /**
  * 文件请求处理
  * 
@@ -35,10 +40,15 @@ public class SysFileController
         try
         {
             // 上传并返回访问地址
-            String url = sysFileService.uploadFile(file);
+            List<String> urlList = sysFileService.uploadFile(file);
+
             SysFile sysFile = new SysFile();
-            sysFile.setName(FileUtils.getName(url));
-            sysFile.setUrl(url);
+            String staticPath = urlList.get(0);
+            String absolutePath = urlList.get(1);
+            sysFile.setName(FileUtils.getName(staticPath));
+            sysFile.setUrl(staticPath);
+            sysFile.setAbsolutePath(absolutePath);
+
             return R.ok(sysFile);
         }
         catch (Exception e)
@@ -54,10 +64,14 @@ public class SysFileController
     @PostMapping("/upload/base64")
     public AjaxResult uploadFileBase64(@RequestBody String base64) {
         try {
-            String url = sysFileService.uploadFileBase64(base64);
+            List<String> urlList = sysFileService.uploadFileBase64(base64);
+            String staticPath = urlList.get(0);
+            String absolutePath = urlList.get(1);
+
             AjaxResult ajax = AjaxResult.success();
-            ajax.put("name", FileUtils.getName(url));
-            ajax.put("url", url);
+            ajax.put("name", FileUtils.getName(staticPath));
+            ajax.put("url", staticPath);
+            ajax.put("absolutePath", absolutePath);
             return ajax;
         } catch (Exception e) {
             return AjaxResult.error(e.getMessage());
@@ -65,4 +79,12 @@ public class SysFileController
     }
 
 
+    /**
+     * 通用上传请求
+     */
+    @PostMapping("/previewAndDownload")
+    public void previewAndDownload(@RequestBody FileDto dto, HttpServletResponse response) throws RuntimeException, IOException {
+        sysFileService.previewAndDownload(dto, response);
+    }
+
 }

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

@@ -3,6 +3,7 @@ package com.boman.file.service;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.domain.dto.AjaxResult;
 import com.boman.domain.dto.ExportExcelDto;
+import com.boman.domain.dto.FileDto;
 import com.boman.domain.dto.ImportExcelDto;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -24,7 +25,7 @@ public interface ISysFileService
      * @return 访问地址
      * @throws Exception
      */
-    public String uploadFile(MultipartFile file) throws Exception;
+    public List<String>  uploadFile(MultipartFile file) throws Exception;
 
     /**
      * 功能描述: 上传base64
@@ -32,7 +33,7 @@ public interface ISysFileService
      * @param base64 base64
      * @return java.lang.String
      */
-    String uploadFileBase64(String base64) throws IOException;
+    List<String> uploadFileBase64(String base64) throws IOException;
 
     /**
      * 功能描述: 通用的导入接口
@@ -61,4 +62,13 @@ public interface ISysFileService
      * @return com.boman.domain.dto.AjaxResult
      */
     AjaxResult exportExcelCommon(HttpServletResponse response, ExportExcelDto dto);
+
+    /**
+     * 功能描述: 查看BooleanUtils.isTrue(dto.getPreview())
+     *          下载BooleanUtils.isFalse(dto.getPreview())
+     *
+     * @param dto      absolutePath preview
+     * @param response response
+     */
+    void previewAndDownload(FileDto dto, HttpServletResponse response) throws IOException;
 }

+ 57 - 14
boman-modules/boman-file/src/main/java/com/boman/file/service/LocalSysFileServiceImpl.java

@@ -3,17 +3,16 @@ package com.boman.file.service;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.common.core.utils.poi.ExcelUtil;
-import com.boman.domain.dto.AjaxResult;
+import com.boman.domain.dto.*;
 import com.boman.common.redis.RedisKey;
 import com.boman.common.redis.service.RedisService;
 import com.boman.domain.GenTable;
 import com.boman.domain.GenTableColumn;
 import com.boman.domain.constant.MaskConstant;
-import com.boman.domain.dto.ExportExcelDto;
-import com.boman.domain.dto.FormDataDto;
-import com.boman.domain.dto.ImportExcelDto;
 import com.boman.file.utils.FileUploadUtils;
 import com.boman.web.core.api.RemoteObjService;
+import com.google.common.collect.Lists;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.BooleanUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -25,7 +24,8 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
+import java.io.*;
+import java.net.URL;
 import java.util.*;
 
 import static com.boman.common.core.utils.obj.ObjectUtils.map;
@@ -74,12 +74,12 @@ public class LocalSysFileServiceImpl implements ISysFileService
      * @throws Exception
      */
     @Override
-    public String uploadFile(MultipartFile file) throws Exception
-    {
+    public List<String> uploadFile(MultipartFile file) throws Exception {
         String name = FileUploadUtils.upload(localFilePath, file);
-        String url = domain + localFilePrefix + name;
-        LOGGER.info("上传的路径为: {}", url);
-        return url;
+        String absolutePath = localFilePath + "\\" + name;
+        String staticUrl = domain + localFilePrefix + name;
+        LOGGER.info("上传静态路径路径为: {}, 绝对路径为: {}", staticUrl, absolutePath);
+        return Lists.newArrayList(staticUrl, absolutePath);
     }
 
     /**
@@ -89,12 +89,13 @@ public class LocalSysFileServiceImpl implements ISysFileService
      * @return java.lang.String
      */
     @Override
-    public String uploadFileBase64(String base64) throws IOException {
+    public List<String>  uploadFileBase64(String base64) throws IOException {
         MultipartFile multipartFile = FileUploadUtils.base64ToMultipart(base64);
         String name = FileUploadUtils.upload(localFilePath, multipartFile);
-        String path = domain + localFilePrefix + name;
-        LOGGER.info("上传的路径为: {}", path);
-        return path;
+        String absolutePath = localFilePath + "\\" + name;
+        String staticUrl = domain + localFilePrefix + name;
+        LOGGER.info("上传静态路径路径为: {}, 绝对路径为: {}", staticUrl, absolutePath);
+        return Lists.newArrayList(staticUrl, absolutePath);
     }
 
     /**
@@ -175,6 +176,48 @@ public class LocalSysFileServiceImpl implements ISysFileService
         return null;
     }
 
+    /**
+     * 功能描述: 查看BooleanUtils.isTrue(dto.getPreview())
+     *          下载BooleanUtils.isFalse(dto.getPreview())
+     *
+     * @param dto      absolutePath preview
+     * @param response response
+     */
+    @Override
+    public void previewAndDownload(FileDto dto, HttpServletResponse response) throws RuntimeException, IOException {
+        String filePath = dto.getAbsolutePath();
+        File file = new File(filePath);
+        if (!file.exists()) {
+            response.sendError(404, "file is not exist, filePath = " + filePath);
+            return;
+        }
+
+        response.reset();
+        if (BooleanUtils.isTrue(dto.getPreview())) {
+            // 查看
+            URL url = new URL("file:///" + filePath);
+            String contentType = url.openConnection().getContentType();
+            response.setContentType(contentType);
+            response.setHeader("Content-Disposition", "inline;filename=test.pdf");
+        } else {
+            // 下载
+            response.setContentType("application/x-msdownload");
+            response.setHeader("Content-Disposition", "attachment;filename=test.pdf");
+        }
+
+        int len;
+        byte[] bs = new byte[1024];
+        BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
+        OutputStream out = response.getOutputStream();
+        while ((len = br.read(bs)) > 0) {
+            out.write(bs, 0, len);
+        }
+
+        out.flush();
+        IOUtils.closeQuietly(out);
+        IOUtils.closeQuietly(br);
+    }
+
     public void handleNullColumnValue(List<Map<String, Object>> result, List<String> showData) {
         for (Map<String, Object> map : result) {
             Set<String> resultKeySet = map.keySet();