瀏覽代碼

文件预览的后端调整

wanyuan 7 月之前
父節點
當前提交
01a0c17b86

+ 51 - 1
td-service/td-apps/src/main/java/com/minto/web/td/TdFileController.java

@@ -49,6 +49,7 @@ import javax.servlet.http.HttpServletResponse;
 import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.*;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 import java.util.List;
 
@@ -756,7 +757,6 @@ public class TdFileController extends TdBaseController {
                     response.setHeader("Content-disposition",
                             "inline;filename=\"" + new String(fileName.getBytes("utf-8"), "iso-8859-1") + "\"");
                     SystemUtil.writeStream(is, response.getOutputStream());
-
                 }
 
             } catch (Exception e) {
@@ -770,4 +770,54 @@ public class TdFileController extends TdBaseController {
         }
         return null;
     }
+
+    @ResponseBody
+    @Operation(summary = "word 转 pdf ")
+    @GetMapping("/showWord")
+    public ModelAndView showWord(HttpServletRequest request, HttpServletResponse response)
+            throws BusinessException{
+        InputStream is = null;
+        try{
+            ISpaceManager manager = AppContext.getBean(ISpaceManager.class);
+            String fileId = ReqUtil.getString(request, "fileId");
+            if(StringUtil.isNotEmpty(fileId)){
+                Long fid = takeFileId(fileId);//得到文件id
+                SpaceFileBO bo = manager.findSpaceFileBO(fid);
+                String wordFilePath = bo.getFile().getPath();
+                String fileName = bo.getSpaceFileBean().getFileName()
+                        .replace(bo.getSpaceFileBean().getFileType(), "pdf");
+                String pdfFilePath = wordFilePath.replace(fileId, fileName);
+                String fileType = bo.getSpaceFileBean().getFileType();
+                if("docx".equals(fileType)){
+                    Docx4jOfficeFileToPDF.docxToPdf(wordFilePath, pdfFilePath, DocumentType.DOCX);
+                } else if("doc".equals(fileType)){
+                    Docx4jOfficeFileToPDF.docxToPdf(wordFilePath, pdfFilePath, DocumentType.DOC);
+                } else if("xlsx".equals(fileType)){
+                    Docx4jOfficeFileToPDF.xlsxToPdf(wordFilePath, pdfFilePath, DocumentType.XLSX);
+                } else if("xls".equals(fileType)){
+                    Docx4jOfficeFileToPDF.xlsxToPdf(wordFilePath, pdfFilePath, DocumentType.XLS);
+                } else if("pptx".equals(fileType) || "ppt".equals(fileType)){
+                    Docx4jOfficeFileToPDF.pptToPdf(bo.getFile(), wordFilePath, pdfFilePath);
+                }else if("pdf".equals(fileType)){
+                    pdfFilePath = bo.getFile().getPath();
+                }
+                //获取到的转换后的pdf路径
+                File previewFile = new File(pdfFilePath);
+                is = new FileInputStream(previewFile);
+                response.setContentType(SpaceEnum.MimeTypeEnum.pdf.getDes());
+                response.setHeader("Content-disposition",
+                        "inline;filename=\"" + new String(fileName.getBytes(StandardCharsets.UTF_8),
+                                StandardCharsets.UTF_8) + "\"");
+                SystemUtil.writeStream(is, response.getOutputStream());
+            }
+        } catch(Exception e){
+            log.error(ExceptionUtil.printExceptionStackTrace(e));
+
+        } finally{
+            if(is != null){
+                SystemUtil.closeAll(is);
+            }
+        }
+        return null;
+    }
 }

+ 30 - 5
tip-front/src/main/java/com/minto/web/tc/FileController.java

@@ -16,28 +16,30 @@ import com.minto.app.space.bo.SpaceFileBO;
 import com.minto.app.space.beans.SpaceFileBean;
 import com.minto.app.task.util.TaskByTypeUtil;
 import com.minto.core.common.AppContext;
+import com.minto.core.util.*;
 import com.minto.tip.common.exceptions.BusinessException;
-import com.minto.core.util.CollectionUtil;
-import com.minto.core.util.DateUtil;
-import com.minto.core.util.ReqUtil;
-import com.minto.core.util.StringUtil;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Restrictions;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.servlet.ModelAndView;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 @Controller
 @RequestMapping("/file")
 public class FileController {
+    @Autowired
+    private ISpaceManager spaceManager;
 
     /**
      * 增加文件查看方式
@@ -53,7 +55,6 @@ public class FileController {
             Map<String,Object> result= Maps.newHashMap();
             Boolean openFlag= ReqUtil.getBoolean(request,"openFlag",false);
             Long fileId=ReqUtil.getLong(request,"fileId");
-            ISpaceManager spaceManager= AppContext.getBean(ISpaceManager.class);
             IBehManager behManager=AppContext.getBean(IBehManager.class);
             IOrgManager orgManager=AppContext.getBean(IOrgManager.class);
             SpaceFileBean fileBean= spaceManager.findSpaceFileBean(fileId);
@@ -177,4 +178,28 @@ public class FileController {
         return  null;
     }
 
+    @GetMapping(value = "/isOnlineShow")
+    public void findFileType(HttpServletRequest request, HttpServletResponse response) throws Exception{
+        List<String> onShowTypes = Lists.newArrayList();
+        onShowTypes.addAll(Arrays.asList(KmEnum.ArcSummaryTypeEnum.Excel.getFtype().split(",")));
+        onShowTypes.addAll(Arrays.asList(KmEnum.ArcSummaryTypeEnum.Word.getFtype().split(",")));
+        onShowTypes.addAll(Arrays.asList(KmEnum.ArcSummaryTypeEnum.PPT.getFtype().split(",")));
+        onShowTypes.addAll(Arrays.asList(KmEnum.ArcSummaryTypeEnum.Pdf.getFtype().split(",")));
+        Map<String, Object> result = new HashMap<String, Object>();
+        try{
+            Long fileId = ReqUtil.getLong(request, "fileId");
+            SpaceFileBean fileBean = spaceManager.findSpaceFileBean(fileId);
+            if(fileBean == null){
+                throw new BusinessException("文件不存在");
+            }
+            result.put("onlineShow", onShowTypes.contains(fileBean.getFileType()));
+            result.put("state", "success");
+        } catch(Exception e){
+            result.put("state", "error");
+            result.put("message", "参数非法,修改失败");
+        } finally{
+            RespUtil.rendJson(response, result);
+        }
+    }
+
 }