From ef26bf4fde77ba361bcd083a769c3a1b8a229738 Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期一, 10 二月 2025 14:39:24 +0800 Subject: [PATCH] excel文件预览使用pdf --- src/main/java/com/product/file/util/AsposeUtil.java | 24 ++++------- src/main/java/com/product/file/service/FileManagerService.java | 43 ++++++++++++++------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/product/file/service/FileManagerService.java b/src/main/java/com/product/file/service/FileManagerService.java index cd6e528..9dd7f0b 100644 --- a/src/main/java/com/product/file/service/FileManagerService.java +++ b/src/main/java/com/product/file/service/FileManagerService.java @@ -21,14 +21,10 @@ import com.product.file.util.FileUtils; import com.product.module.sys.entity.SystemUser; import com.product.util.BaseUtil; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; @@ -469,7 +465,11 @@ String viewOnlineSign = attachmentFse.getString(CmnConst.VIEW_ONLINE_SIGN); boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN)); boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.NEED_ONLINE_VIEW)) && "1".equals(viewOnlineSign); - if (needOnlineViewFlag && !realFileName.endsWith(".xlsx") && !realFileName.endsWith(".xls")) { +// if (needOnlineViewFlag && !realFileName.endsWith(".xlsx") && !realFileName.endsWith(".xls")) { +// // 闇�瑕佸湪绾块瑙堜笖杞崲涔嬪悗鎵嶈兘鍦ㄧ嚎棰勮 excel 鏂囦欢涓嶉渶瑕佽浆鎹㈢洿鎺ヨ緭鍑洪瑙� +// dir += File.separator + CmnConst.TRANSFER_DIR_NAME; +// } + if (needOnlineViewFlag) { // 闇�瑕佸湪绾块瑙堜笖杞崲涔嬪悗鎵嶈兘鍦ㄧ嚎棰勮 excel 鏂囦欢涓嶉渶瑕佽浆鎹㈢洿鎺ヨ緭鍑洪瑙� dir += File.separator + CmnConst.TRANSFER_DIR_NAME; } @@ -480,11 +480,13 @@ FTPService ftpService = new FTPService(); logger.info("闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠�..."); response.setHeader("Access-Control-Expose-Headers", "*"); - if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) { - response.setContentType("application/vnd.ms-excel"); - } else { - response.setContentType("multipart/form-data"); - } +// if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) { +// response.setContentType("application/vnd.ms-excel"); +// } else { +// response.setContentType("multipart/form-data"); +// } + + response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8")); //璁剧疆澶翠腑鐨勬枃浠剁被鍨� if (!needOnlineViewFlag && !encrptSignFlag) { @@ -535,6 +537,15 @@ String localBasePath = Global.getSystemConfig("local.dir", ""); path = localBasePath + File.separator + path; File file = new File(path); + if (needOnlineViewFlag && file.exists() && (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls"))) { + //鑾峰彇鏂囦欢鐨勪慨鏀规椂闂� + long fileTime = file.lastModified(); + //濡傛灉鏃堕棿鏄� 2025骞�2鏈�10鏃�23:59:59锛岄偅涔堝氨閲嶆柊杞崲 + //鍒犻櫎鏂囦欢 + if (fileTime <= 1739203198000L) { + file.delete(); + } + } if (needOnlineViewFlag && !file.exists()) { // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣� File sourceFile = fileUtils.getFile(false, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag); @@ -547,11 +558,13 @@ byte[] b = new byte[1024]; InputStream is = new FileInputStream(file); response.setHeader("Access-Control-Expose-Headers", "*"); - if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) { - response.setContentType("application/vnd.ms-excel"); - } else { - response.setContentType("multipart/form-data"); - } +// if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) { +// response.setContentType("application/vnd.ms-excel"); +// } else { +// response.setContentType("multipart/form-data"); +// } + + response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8")); response.setContentLengthLong(file.length()); try (ServletOutputStream os = response.getOutputStream()) { diff --git a/src/main/java/com/product/file/util/AsposeUtil.java b/src/main/java/com/product/file/util/AsposeUtil.java index 25e6f39..c35bbfc 100644 --- a/src/main/java/com/product/file/util/AsposeUtil.java +++ b/src/main/java/com/product/file/util/AsposeUtil.java @@ -1,24 +1,18 @@ package com.product.file.util; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import com.aspose.cells.Cells; import com.aspose.cells.PdfSaveOptions; -import com.aspose.cells.Workbook; -import com.aspose.cells.Worksheet; -import com.aspose.cells.WorksheetCollection; +import com.aspose.cells.*; import com.aspose.slides.Presentation; +import com.aspose.words.SaveFormat; import com.aspose.words.*; import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.file.config.FileCode; + +import java.io.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; public class AsposeUtil { private static int OFFICE_XLS = 1; @@ -293,8 +287,8 @@ if (officeType.equals("doc") || officeType.equals("docx")) { Word2Pdf(officePath, outFile); } else if (officeType.equals("xls") || officeType.equals("xlsx")) { -// Excel2Pdf(officePath,outFile); - Excel2Html(officePath, outFile); + Excel2Pdf(officePath,outFile); +// Excel2Html(officePath, outFile); // xls2xlsx(officePath,outFile,officeType); } else if (officeType.equals("ppt") || officeType.equals("pptx")) { PPT2Pdf(officePath, outFile); -- Gitblit v1.9.2