Browse Source

通告管理在线预览,明华的定时任务cron表达式错误导致无法启动服务,注释掉

hlf 2 years ago
parent
commit
2e9bf8b53a

+ 8 - 3
ims-service/ims-eval/pom.xml

@@ -76,9 +76,14 @@
             <version>2.3.28</version>
         </dependency>
         <dependency>
-            <groupId>com.artofsolving</groupId>
-            <artifactId>jodconverter</artifactId>
-            <version>2.2.1</version>
+            <groupId>org.apache.pdfbox</groupId>
+            <artifactId>pdfbox</artifactId>
+            <version>2.0.4</version>
+        </dependency>
+        <dependency>
+            <groupId>fr.opensagres.xdocreport</groupId>
+            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
+            <version>2.0.1</version>
         </dependency>
     </dependencies>
     <build>

+ 19 - 26
ims-service/ims-eval/src/main/java/com/ims/eval/controller/NoticeManagementController.java

@@ -17,11 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -250,35 +247,31 @@ public class NoticeManagementController {
 	/**
 	 * 文件预览
 	 *
-	 * @param url      url
-	 * @param response response
+	 * @param url 文件路径
 	 * @return 结果
-	 * @throws Exception
+	 * @throws IOException
 	 */
 	@GetMapping(value = "/filePreview")
-	public void filePreview(@RequestParam(value = "url") String url, HttpServletResponse response) throws Exception {
-		InputStream in = null;
+	public R filePreview(@RequestParam(value = "url") String url) {
+		File inputPath = new File(Path.getNoticePath() + url);
+		String prefix = url.substring(0, url.lastIndexOf(".") - 1);
+		String suffix = ".pdf";
+		String outputPath = Path.getNoticePath() + prefix + suffix;
 		try {
-			in = FileConvertUtil.convertLocaleFile(url, "pdf");
-			OutputStream outputStream = response.getOutputStream();
-			//创建存放文件内容的数组
-			byte[] buff = new byte[1024];
-			//所读取的内容使用n来接收
-			int n;
-			//当没有读取完时,继续读取,循环
-			while ((n = in.read(buff)) != -1) {
-				//将字节数组的数据全部写入到输出流中
-				outputStream.write(buff, 0, n);
+			if (url.endsWith(".doc")) {
+				FileConvertUtil.convertDocToPdf(inputPath, outputPath);
+			} else if (url.endsWith(".docx")) {
+				FileConvertUtil.convertDocxToPdf(inputPath, outputPath);
+			} else if (url.endsWith(".xls")) {
+				FileConvertUtil.convertExcelToPdf(inputPath, outputPath);
+			} else if (url.endsWith(".xlsx")) {
+				FileConvertUtil.convertExcelToPdf(inputPath, outputPath);
+			} else {
+				throw new Exception("不支持的文件格式");
 			}
-			//强制将缓存区的数据进行输出
-			outputStream.flush();
-			//关流
-			outputStream.close();
-			in.close();
-			//return R.ok();
 		} catch (Exception e) {
-			log.error("错误", e);
+			e.printStackTrace();
 		}
-		//evaluationNoticeService.onlinePreview(Path.getNoticePath() + url, response);
+		return R.ok().data(outputPath);
 	}
 }

+ 2 - 2
ims-service/ims-eval/src/main/java/com/ims/eval/schedule/EvaluationWarningSchedule.java

@@ -90,7 +90,7 @@ public class EvaluationWarningSchedule {
 	/**
 	 * 单位年度考评
 	 */
-	@Scheduled(cron = "* * 8 1 4 ? 2023-2050") // cron表达式: 每年4月1号8点触发
+	//@Scheduled(cron = "* * 8 1 4 ? 2023-2050") // cron表达式: 每年4月1号8点触发
 	public void warningEvaluationNDKP(){
 		Calendar cal = Calendar.getInstance();
 		String year = String.valueOf(cal.get(Calendar.YEAR));
@@ -153,7 +153,7 @@ public class EvaluationWarningSchedule {
 	/**
 	 * 部门年度考评
 	 */
-	@Scheduled(cron = "* * 8 1 4 ? 2023,2050") // cron表达式: 每年4月1号8点触发
+	//@Scheduled(cron = "* * 8 1 4 ? 2023,2050") // cron表达式: 每年4月1号8点触发
 	public void warningEvaluationDeptYDKP(){
 		Calendar cal = Calendar.getInstance();
 		String year = String.valueOf(cal.get(Calendar.YEAR));

+ 0 - 3
ims-service/ims-eval/src/main/java/com/ims/eval/service/IEvaluationNoticeService.java

@@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.ims.eval.entity.EvaluationNotice;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 
 /**
@@ -18,6 +17,4 @@ public interface IEvaluationNoticeService extends IService<EvaluationNotice> {
 	IPage<EvaluationNotice> listPage(Integer pageNum, Integer pageSize, String noticeTitle);
 
 	boolean saveOrUpdate(EvaluationNotice evaluationNotice, MultipartFile[] files) throws IOException;
-
-	void onlinePreview(String url, HttpServletResponse response) throws Exception;
 }

+ 0 - 43
ims-service/ims-eval/src/main/java/com/ims/eval/service/impl/EvaluationNoticeServiceImpl.java

@@ -7,15 +7,11 @@ import com.ims.eval.dao.EvaluationNoticeMapper;
 import com.ims.eval.entity.EvaluationNotice;
 import com.ims.eval.entity.custom.Path;
 import com.ims.eval.service.IEvaluationNoticeService;
-import com.ims.eval.util.FileConvertUtil;
 import com.ims.eval.util.FileUploadUtil;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.util.Date;
 
 /**
@@ -68,43 +64,4 @@ public class EvaluationNoticeServiceImpl extends ServiceImpl<EvaluationNoticeMap
 		}
 		return super.saveOrUpdate(evaluationNotice);
 	}
-
-	/**
-	 * @Description:文件在线预览接口
-	 */
-	@Override
-	public void onlinePreview(String url, HttpServletResponse response) throws Exception {
-		/*//获取文件类型
-		String[] str = StringUtils.split(url, "\\.");
-
-		if (str.length == 0) {
-			throw new Exception("文件格式不正确");
-		}
-		String suffix = str[str.length - 1];
-		if (!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls") && !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx")) {
-			throw new Exception("文件格式不支持预览");
-		}
-		InputStream in = FileConvertUtil.convertNetFile(url, suffix);*/
-		InputStream in = null;
-		try {
-			in = FileConvertUtil.convertLocaleFile(url, "pdf");
-			OutputStream outputStream = response.getOutputStream();
-			//创建存放文件内容的数组
-			byte[] buff = new byte[1024];
-			//所读取的内容使用n来接收
-			int n;
-			//当没有读取完时,继续读取,循环
-			while ((n = in.read(buff)) != -1) {
-				//将字节数组的数据全部写入到输出流中
-				outputStream.write(buff, 0, n);
-			}
-			//强制将缓存区的数据进行输出
-			outputStream.flush();
-			//关流
-			outputStream.close();
-			in.close();
-		} catch (Exception e) {
-			log.error("错误", e);
-		}
-	}
 }

+ 56 - 68
ims-service/ims-eval/src/main/java/com/ims/eval/util/FileConvertUtil.java

@@ -1,94 +1,82 @@
 package com.ims.eval.util;
 
-import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
-import com.artofsolving.jodconverter.DocumentConverter;
-import com.artofsolving.jodconverter.DocumentFormat;
-import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
-import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
-import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
+import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
 
 import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLConnection;
-import java.nio.file.Files;
 
 /**
  * @author hlf
- * @date 2023/5/25 14:22
- * 文件说明:文件格式转换工具类
+ * @date 2023/5/25 17:45
+ * 文件说明:
  */
 public class FileConvertUtil {
 
 	/**
-	 * 默认转换后文件后缀
-	 */
-	private static final String DEFAULT_SUFFIX = "pdf";
-	/**
-	 * openoffice_port
-	 */
-	private static final Integer OPENOFFICE_PORT = 8100;
-
-	/**
-	 * 方法描述 office文档转换为PDF(处理本地文件)
+	 * 将 DOCX 文件转换为 PDF 格式并保存在上传路径下
 	 *
-	 * @param sourcePath 源文件路径
-	 * @param suffix     源文件后缀
-	 * @return InputStream 转换后文件输入流
+	 * @param docxFile 需要转换的 DOCX 文件
+	 * @param pdfPath  转换生成的 PDF 文件在服务器中存放的路径
+	 * @throws IOException 抛出 IO 异常
 	 */
-	public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {
-		File inputFile = new File(sourcePath);
-		InputStream inputStream = Files.newInputStream(inputFile.toPath());
-		return covertCommonByStream(inputStream, suffix);
+	public static void convertDocxToPdf(File docxFile, String pdfPath) throws IOException {
+		try (InputStream inputStream = new FileInputStream(docxFile)) {
+			XWPFDocument document = new XWPFDocument(inputStream);
+			File targetFile = new File(pdfPath);
+			targetFile.getParentFile().mkdirs();
+			try (OutputStream outputStream = new FileOutputStream(targetFile)) {
+				PdfConverter.getInstance().convert(document, outputStream, null);
+			}
+		}
 	}
 
 	/**
-	 * 方法描述  office文档转换为PDF(处理网络文件)
+	 * 将 XLS、XLSX 文件转换为 PDF 格式并保存在上传路径下
 	 *
-	 * @param netFileUrl 网络文件路径
-	 * @param suffix     文件后缀
-	 * @return InputStream 转换后文件输入流
+	 * @param excelFile 需要转换的 Excel 文件对象
+	 * @param pdfPath   转换生成的 PDF 文件在服务器中存放的路径
+	 * @throws IOException 抛出 IO 异常
 	 */
-	public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception {
-		// 创建URL
-		URL url = new URL(netFileUrl);
-		// 试图连接并取得返回状态码
-		URLConnection urlconn = url.openConnection();
-		urlconn.connect();
-		HttpURLConnection httpconn = (HttpURLConnection) urlconn;
-		int httpResult = httpconn.getResponseCode();
-		if (httpResult == HttpURLConnection.HTTP_OK) {
-			InputStream inputStream = urlconn.getInputStream();
-			return covertCommonByStream(inputStream, suffix);
+	public static void convertExcelToPdf(File excelFile, String pdfPath) throws IOException, InvalidFormatException {
+		try (InputStream inputStream = new FileInputStream(excelFile)) {
+			Workbook workbook = WorkbookFactory.create(inputStream);
+			PDDocument pdfDocument = new PDDocument();
+			for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
+				PDPage page = new PDPage();
+				pdfDocument.addPage(page);
+				PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
+				contentStream.beginText();
+				contentStream.setFont(PDType1Font.TIMES_BOLD, 12);
+				contentStream.setLeading(14.5f);
+				contentStream.newLineAtOffset(25, 725);
+				String[] sheetData = workbook.getSheetAt(i).getRow(0).toString().split(",");
+				for (String data : sheetData) {
+					contentStream.showText(data.trim());
+					contentStream.newLine();
+				}
+				contentStream.endText();
+				contentStream.close();
+			}
+			pdfDocument.save(pdfPath);
+			pdfDocument.close();
 		}
-		return null;
 	}
 
 	/**
-	 * 方法描述  将文件以流的形式转换
+	 * 将 DOC 文件转换为 PDF 格式并保存在上传路径下
 	 *
-	 * @param inputStream 源文件输入流
-	 * @param suffix      源文件后缀
-	 * @return InputStream 转换后文件输入流
-	 */
-	public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception {
-		ByteArrayOutputStream out = new ByteArrayOutputStream();
-		OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_PORT);
-		connection.connect();
-		DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
-		DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
-		DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX);
-		DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix);
-		converter.convert(inputStream, sourceFormat, out, targetFormat);
-		connection.disconnect();
-		return outputStreamConvertInputStream(out);
-	}
-
-	/**
-	 * 方法描述 outputStream转inputStream
+	 * @param docFile 需要转换的 DOC 文件对象
+	 * @param pdfPath 转换生成的 PDF 文件在服务器中存放的路径
+	 * @throws IOException 抛出 IO 异常
 	 */
-	public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {
-		ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
-		return new ByteArrayInputStream(baos.toByteArray());
+	public static void convertDocToPdf(File docFile, String pdfPath) throws IOException {
+		convertDocxToPdf(docFile, pdfPath);
 	}
 }