|
|
|
@ -1,5 +1,10 @@ |
|
|
|
package com.xujie.sys.modules.oss.service.impl; |
|
|
|
|
|
|
|
import com.aspose.cells.PageOrientationType; |
|
|
|
import com.aspose.cells.PageSetup; |
|
|
|
import com.aspose.cells.Worksheet; |
|
|
|
import com.aspose.cells.Workbook; |
|
|
|
import com.aspose.cells.PaperSizeType; |
|
|
|
import com.xujie.sys.datasource.annotation.DataSource; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
@ -27,6 +32,13 @@ import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.*; |
|
|
|
import com.aspose.words.Document; |
|
|
|
|
|
|
|
import com.aspose.cells.Workbook; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
|
|
|
|
@Service("sysOssService") |
|
|
|
|
|
|
|
@ -141,7 +153,7 @@ public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> impl |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void previewOssFileById(Integer id, HttpServletResponse response) { |
|
|
|
public void previewOssFileById(Integer id, HttpServletResponse response) throws Exception { |
|
|
|
SysOssEntity oss = this.getById(id); |
|
|
|
if (Objects.isNull(oss)) { |
|
|
|
throw new IllegalArgumentException("文件不存在"); |
|
|
|
@ -152,10 +164,27 @@ public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> impl |
|
|
|
if (!file.exists()) { |
|
|
|
throw new IllegalArgumentException("文件不存在"); |
|
|
|
} |
|
|
|
if (!new File(oss.getUrl().split("\\.")[0] + ".pdf").exists()) { |
|
|
|
// 获取文件扩展名 |
|
|
|
String fileExtension = getFileExtension(file); |
|
|
|
|
|
|
|
if ("xlsx".equalsIgnoreCase(fileExtension) || "xls".equalsIgnoreCase(fileExtension)) { |
|
|
|
// 如果是 Excel 文件,将其转换为 PDF |
|
|
|
convertExcelToPdf(file); |
|
|
|
} else if ("docx".equalsIgnoreCase(fileExtension)) { |
|
|
|
// 如果是 Word 文件,将其转换为 PDF |
|
|
|
convertWordToPdf(file); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
String encodedFileName; |
|
|
|
String fileName = oss.getFileName(); |
|
|
|
try { |
|
|
|
encodedFileName = URLEncoder.encode(oss.getFileName(), "UTF-8"); |
|
|
|
// 如果oss.getFileName()的扩展名为xlsx或xls或docx或doc,则将文件名后缀改为pdf |
|
|
|
if (fileName.endsWith(".xlsx") || fileName.endsWith(".xls") || fileName.endsWith(".docx") || fileName.endsWith(".doc")) { |
|
|
|
fileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf"; |
|
|
|
} |
|
|
|
encodedFileName = URLEncoder.encode(fileName, "UTF-8"); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
throw new RuntimeException("文件名编码失败", e); |
|
|
|
} |
|
|
|
@ -168,6 +197,13 @@ public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> impl |
|
|
|
} |
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName); |
|
|
|
|
|
|
|
|
|
|
|
if (oss.getFileType().equals("xlsx") || oss.getFileType().equals("xls") || oss.getFileType().equals("docx") || oss.getFileType().equals("doc")) { |
|
|
|
file = new File(oss.getUrl().split("\\.")[0] + ".pdf"); |
|
|
|
} else { |
|
|
|
file = new File(oss.getUrl()); |
|
|
|
} |
|
|
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(file); |
|
|
|
BufferedInputStream bis = new BufferedInputStream(fis); |
|
|
|
ServletOutputStream os = response.getOutputStream()) { |
|
|
|
@ -183,6 +219,60 @@ public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> impl |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException("文件下载失败", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 获取文件扩展名的方法 |
|
|
|
public static String getFileExtension(File file) { |
|
|
|
String fileName = file.getName(); |
|
|
|
int lastIndexOfDot = fileName.lastIndexOf('.'); |
|
|
|
if (lastIndexOfDot == -1) { |
|
|
|
return ""; // 如果没有扩展名,返回空字符串 |
|
|
|
} |
|
|
|
return fileName.substring(lastIndexOfDot + 1); |
|
|
|
} |
|
|
|
|
|
|
|
// 将 Excel 文件转换为 PDF |
|
|
|
public static void convertExcelToPdf(File file) throws Exception { |
|
|
|
Workbook workbook; |
|
|
|
String filePath = file.getAbsolutePath(); |
|
|
|
|
|
|
|
if (filePath.endsWith(".xlsx")) { |
|
|
|
workbook = new Workbook(file.getAbsolutePath()); |
|
|
|
} else { |
|
|
|
workbook = new Workbook(filePath); // 使用 Aspose.Cells 读取 XLS 文件 |
|
|
|
} |
|
|
|
|
|
|
|
Worksheet worksheet = workbook.getWorksheets().get(0); |
|
|
|
PageSetup pageSetup = worksheet.getPageSetup(); |
|
|
|
|
|
|
|
// 设置页面方向(横向或纵向) |
|
|
|
pageSetup.setOrientation(PageOrientationType.PORTRAIT); // 纵向 |
|
|
|
// pageSetup.setOrientation(PageOrientationType.LANDSCAPE); // 横向 |
|
|
|
|
|
|
|
// 设置纸张大小(例如 A4) |
|
|
|
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4); |
|
|
|
// 设置缩放比例(例如调整为 80% 的原始大小) |
|
|
|
pageSetup.setZoom(60); |
|
|
|
|
|
|
|
// 其他设置,比如边距 |
|
|
|
pageSetup.setLeftMargin(0); // 左边距 |
|
|
|
pageSetup.setRightMargin(0); // 右边距 |
|
|
|
pageSetup.setTopMargin(0.5); // 上边距 |
|
|
|
pageSetup.setBottomMargin(0.5); // 下边距 |
|
|
|
|
|
|
|
// 转换为 PDF |
|
|
|
String pdfPath = filePath.replaceAll("\\.xlsx|\\.xls", ".pdf"); |
|
|
|
workbook.save(pdfPath, com.aspose.cells.SaveFormat.PDF); |
|
|
|
System.out.println("Excel 文件转换为 PDF 成功,保存路径:" + pdfPath); |
|
|
|
} |
|
|
|
|
|
|
|
// 将 Word 文件转换为 PDF |
|
|
|
public static void convertWordToPdf(File file) throws Exception { |
|
|
|
Document document = new Document(file.getAbsolutePath()); |
|
|
|
|
|
|
|
// 转换为 PDF |
|
|
|
String pdfPath = file.getAbsolutePath().replace(".docx", ".pdf"); |
|
|
|
document.save(pdfPath); |
|
|
|
System.out.println("Word 文件转换为 PDF 成功,保存路径:" + pdfPath); |
|
|
|
} |
|
|
|
} |