瀏覽代碼

文件PDF修改为预览模式

chenjun 8 月之前
父節點
當前提交
883109f3e2

+ 17 - 0
zjugis-framework/zjugis-common/src/main/java/com/zjugis/framework/common/util/servlet/ServletUtils.java

@@ -50,6 +50,23 @@ public class ServletUtils {
         IoUtil.write(response.getOutputStream(), false, content);
     }
 
+    /**
+     * 返回附件
+     *
+     * @param response 响应
+     * @param filename 文件名
+     * @param content 附件内容
+     */
+    public static void writeInline(HttpServletResponse response, String filename, byte[] content) throws IOException {
+        // 设置 header 和 contentType
+        response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(filename, "UTF-8"));
+        response.setContentType(MediaType.APPLICATION_PDF_VALUE);
+        // 输出附件
+        IoUtil.write(response.getOutputStream(), false, content);
+    }
+
+
+
     /**
      * @param request 请求
      * @return ua

+ 11 - 9
zjugis-module-infra/zjugis-module-infra-biz/src/main/java/com/zjugis/module/infra/controller/admin/file/FileController.java

@@ -28,10 +28,6 @@ import javax.annotation.security.PermitAll;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
 import java.net.URLEncoder;
 import java.util.List;
 
@@ -89,7 +85,7 @@ public class FileController {
     @GetMapping("/{configId}/get/**")
     @PermitAll
     @Operation(summary = "下载文件")
-    @Parameter(name = "configId", description = "配置编号",  required = true)
+    @Parameter(name = "configId", description = "配置编号", required = true)
     public void getFileContent(HttpServletRequest request,
                                HttpServletResponse response,
                                @PathVariable("configId") Long configId) throws Exception {
@@ -106,16 +102,22 @@ public class FileController {
             response.setStatus(HttpStatus.NOT_FOUND.value());
             return;
         }
-        ServletUtils.writeAttachment(response, path, content);
+        int lastDotIndex = path.lastIndexOf('.');
+        String type = path.substring(lastDotIndex + 1).toLowerCase();
+        if ("pdf".equals(type)) {
+            ServletUtils.writeInline(response, path, content);
+        } else {
+            ServletUtils.writeAttachment(response, path, content);
+        }
     }
 
     @GetMapping("/{configId}/get/video/**")
     @PermitAll
     @Operation(summary = "下载文件")
-    @Parameter(name = "configId", description = "配置编号",  required = true)
+    @Parameter(name = "configId", description = "配置编号", required = true)
     public void getVideoFileContent(HttpServletRequest request,
-                               HttpServletResponse response,
-                               @PathVariable("configId") Long configId) throws Exception {
+                                    HttpServletResponse response,
+                                    @PathVariable("configId") Long configId) throws Exception {
         // 获取请求的路径
         String path = StrUtil.subAfter(request.getRequestURI(), "/get/video/", false);
         if (StrUtil.isEmpty(path)) {