From c28e9990179f5573ea94f41130fcec8c64f7902a Mon Sep 17 00:00:00 2001 From: shichongfu <shi_chongfu@163.com> Date: 星期三, 07 六月 2023 17:09:14 +0800 Subject: [PATCH] 文档转换新转换方式,在properties中配置,open office是否启用 --- src/main/java/com/product/file/util/AsposeUtil.java | 269 ++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 16 ++ src/main/java/com/product/file/util/PdfConcurrenceUtil.java | 12 + src/main/java/com/product/file/util/FileUtils.java | 21 ++ 4 files changed, 307 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 262c1fd..1b50b46 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,6 @@ <artifactId>product-server-file</artifactId> <name>product-server-file</name> <description>product-server-file</description> - <version>2.0.0-release</version> <dependencies> <dependency> <groupId>com.lx</groupId> @@ -81,6 +80,21 @@ <artifactId>pdfbox</artifactId> <version>2.0.6</version> </dependency> + <dependency> + <groupId>org.aspose</groupId> + <artifactId>cells-line</artifactId> + <version>15.9.0</version> + </dependency> + <dependency> + <groupId>org.aspose</groupId> + <artifactId>slides-line</artifactId> + <version>15.9.0</version> + </dependency> + <dependency> + <groupId>org.aspose</groupId> + <artifactId>words-line</artifactId> + <version>15.9.0</version> + </dependency> </dependencies> </project> diff --git a/src/main/java/com/product/file/util/AsposeUtil.java b/src/main/java/com/product/file/util/AsposeUtil.java new file mode 100644 index 0000000..152695e --- /dev/null +++ b/src/main/java/com/product/file/util/AsposeUtil.java @@ -0,0 +1,269 @@ +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.math.RoundingMode; +import java.text.DecimalFormat; + +import com.aspose.cells.PdfSaveOptions; +import com.aspose.cells.Workbook; +import com.aspose.slides.Presentation; +import com.aspose.words.Document; +import com.product.core.exception.BaseException; +import com.product.core.spring.context.SpringMVCContextHolder; + +public class AsposeUtil { + //鏄惁鍒濆浜唋icense + private static boolean isInitLicense=false; + /** + * 鑾峰彇license + * @return + */ + public static boolean getLicense(int type)throws BaseException { + //宸茬粡鍒濆杩囷紝涓嶅啀鍒濆 + if(isInitLicense) { + return true; + } + boolean result = true; + InputStream is = null; + try { + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.cells.License excel = new com.aspose.cells.License(); + excel.setLicense(is); + + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.words.License word = new com.aspose.words.License(); + word.setLicense(is); + + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.slides.License ppt = new com.aspose.slides.License(); + ppt.setLicense(is); + + isInitLicense=true; + } catch (Exception e) { + result=false; + throw new BaseException(e); + }finally{ + if(is!=null) { + try { + is.close(); + }catch(IOException e) { + throw new BaseException(e); + } + } + } + return result; + } + /** + * + * @param officePath + * @param OutPutPath + * @throws BaseException + */ + public static void Excel2Pdf(String officePath,String OutPutPath)throws BaseException { + // 楠岃瘉License + if (!getLicense(1)) { + return; + } + FileOutputStream fileOS=null; + try { + File file = new File(OutPutPath); + if (!file.exists()) { + file.mkdirs(); + } + Workbook wb = new Workbook(officePath);// 鍘熷excel璺緞 + File pdfFile = new File(OutPutPath);// 杈撳嚭璺緞 + fileOS = new FileOutputStream(pdfFile); + //wb.save(fileOS, com.aspose.cells.SaveFormat.PDF); + PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); + pdfSaveOptions.setAllColumnsInOnePagePerSheet(true); + wb.save(fileOS, pdfSaveOptions); + } catch (Exception e) { + throw new BaseException(e); + }finally{ + if(fileOS!=null) { + try { + fileOS.flush(); + fileOS.close(); + }catch(IOException e) { + throw new BaseException(e); + } + } + } + } + /** + * + * @param officePath + * @param OutPutPath + * @throws BaseException + */ + public static void Word2Pdf(String officePath,String OutPutPath) throws BaseException { + // 楠岃瘉License + if (!getLicense(2)) { + return; + } + FileOutputStream fileOS=null; + try { + File file = new File(OutPutPath); + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + Document doc = new Document(officePath);// 鍘熷word璺緞 + File pdfFile = new File(OutPutPath);// 杈撳嚭璺緞 + fileOS = new FileOutputStream(pdfFile); + doc.save(fileOS, com.aspose.words.SaveFormat.PDF); + } catch (Exception e) { + throw new BaseException(e); + }finally{ + if(fileOS!=null) { + try { + fileOS.flush(); + fileOS.close(); + }catch(IOException e) { + throw new BaseException(e); + } + } + } + } + /** + * + * @param officePath + * @param OutPutPath + * @throws BaseException + */ + public static void PPT2Pdf(String officePath,String OutPutPath)throws BaseException { + // 楠岃瘉License + if (!getLicense(3)) { + return; + } + FileOutputStream fileOS=null; + try { + File PathFile = new File(OutPutPath); + if (!PathFile.exists()) { + PathFile.mkdirs(); + } + InputStream slides = new FileInputStream(new File(officePath));// 鍘熷ppt璺緞 + Presentation pres = new Presentation(slides); + File file = new File(OutPutPath);// 杈撳嚭pdf璺緞 + fileOS = new FileOutputStream(file); + pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf); + fileOS.flush(); + fileOS.close(); + } catch (Exception e) { + throw new BaseException(e); + }finally{ + if(fileOS!=null) { + try { + fileOS.flush(); + fileOS.close(); + }catch(IOException e) { + throw new BaseException(e); + } + } + } + } + /** + * 鏂囨。杞崲 + * @param officePath + * @param outFile + * @return + * @throws BaseException + */ + public static String OfficeToPdf(String officePath,String outFile,String officeType )throws BaseException { + + SpringMVCContextHolder.getSystemLogger().info("The file of office type:"+officePath); + SpringMVCContextHolder.getSystemLogger().info("The file of PDF type:"+outFile); + File file = new File(officePath); + //鍒ゆ柇褰撳墠office鏂囦欢鏄惁宸茬粡杞负PDF,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� + if (file.exists()&& !"pdf".equals(officeType)) { + double bytes = file.length(); + double kilobytes = (bytes / 1024); + double megabytes = (kilobytes / 1024); + + DecimalFormat df = new DecimalFormat("0.00"); + df.setRoundingMode(RoundingMode.HALF_UP); + String MB = df.format(megabytes); + Double Size = Double.parseDouble(MB); +// if(Size>10){ + SpringMVCContextHolder.getSystemLogger().info("The file size:"+Size+"MB"); +// } + //"doc", "docx", "xls","xlsx", "ppt", "pptx" + if(officeType.equals("doc")||officeType.equals("docx")){ + Word2Pdf(officePath,outFile); + }else if(officeType.equals("xls")||officeType.equals("xlsx")){ + Excel2Pdf(officePath,outFile); + }else if(officeType.equals("ppt")||officeType.equals("pptx")){ + PPT2Pdf(officePath,outFile); + }else{ + SpringMVCContextHolder.getSystemLogger().info("File types that cannot be converted:"+officeType); + } + } else { + SpringMVCContextHolder.getSystemLogger().info("File Not Exist"); + } + return outFile; + } + /** + * 鏂囨。杞崲 + * @param officePath + * @return + */ + public static String OfficeToPdf(String officePath) { + + //G:/product/WebApp/fwis_develop/com/is/flywings/oa/attchfile/1000000000/i0002/101951.docx鈱�101951.docx鈱抐eiyu.docx + String[] split = officePath.split("鈱�"); + int lastIndex = split[0].lastIndexOf("."); + int lastNameIndex = split[0].lastIndexOf("/"); + + String officeType = split[0].substring(lastIndex+1); + String officeName = split[0].substring(lastNameIndex+1,lastIndex)+".pdf"; + String OutPutPath = split[0].substring(0,lastNameIndex+1)+"office/"; + System.out.println("杈撳嚭鐩綍锛�"+OutPutPath); + File file = new File(split[0]); + File pdfFile = new File(OutPutPath+officeName); + //鍒ゆ柇褰撳墠office鏂囦欢鏄惁宸茬粡杞负PDF,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� + if(pdfFile.exists()){ + return OutPutPath+officeName; + } + + if (file.exists()) { + + double bytes = file.length(); + double kilobytes = (bytes / 1024); + double megabytes = (kilobytes / 1024); + + DecimalFormat df = new DecimalFormat("0.00"); + df.setRoundingMode(RoundingMode.HALF_UP); + String MB = df.format(megabytes); + Double Size = Double.parseDouble(MB); + if(Size>10){ + return Size+"MB"; + } + //"doc", "docx", "xls","xlsx", "ppt", "pptx" + try { + if(officeType.equals("doc")||officeType.equals("docx")){ + Word2Pdf(split[0],OutPutPath+"/"+officeName); + }else if(officeType.equals("xls")||officeType.equals("xlsx")){ + Excel2Pdf(split[0],OutPutPath+"/"+officeName); + }else if(officeType.equals("ppt")||officeType.equals("pptx")){ + PPT2Pdf(split[0],OutPutPath+"/"+officeName); + }else{ + System.out.println("鏃犳硶璇嗗埆璇ユ枃浠讹紒"); + return "Error"; + } + } catch (Exception e) { + e.printStackTrace(); + } + } else { + return "NotExists"; + } + return OutPutPath+officeName; + } + + public static void main(String args[]) { + String file="E:/work/娌勫磧/FE6.6浜у搧鎶ヤ环宸ュ叿-鏂� - 鍓湰.xlsx"; + AsposeUtil.OfficeToPdf(file); + } +} diff --git a/src/main/java/com/product/file/util/FileUtils.java b/src/main/java/com/product/file/util/FileUtils.java index 789d00f..77416f6 100644 --- a/src/main/java/com/product/file/util/FileUtils.java +++ b/src/main/java/com/product/file/util/FileUtils.java @@ -557,9 +557,10 @@ if (placeLocation.indexOf("/") != 0) { placeLocation += "/"; } + //绾跨▼鍚屾 if (threadSync) { - boolean b = convertPdf(uploadServer, isEncrypt, file, placeLocation, fileName); + boolean b = convertPdf(uploadServer, isEncrypt, file, placeLocation, fileName +"."+tail); if (deleteFile && file.isFile()) { file.delete(); } @@ -569,7 +570,7 @@ String finalPlaceLocation = placeLocation; final File ff = FileUtil.copyFile(file, Global.getSystemConfig("temp.dir", "") + "/" + "temp_convert_pdf_" + RandomUtil.randomString(5) + "_" + file.getName(), 0); executorService.submit(() -> { - convertPdf(uploadServer, isEncrypt, ff, finalPlaceLocation, fileName); + convertPdf(uploadServer, isEncrypt, ff, finalPlaceLocation, fileName+"."+tail); if (ff.isFile()) { ff.delete(); } @@ -588,20 +589,30 @@ String tempPath = Global.getSystemConfig("temp.dir", "./attachment/temp") + "/" + "convertPDF_" + random + "_" + file.getName(); String tempPathSuccess = Global.getSystemConfig("temp.dir", "./attachment/temp") + "/" + "convertPDF_success_" + random + ".pdf"; try { + int lastIndex = fileName.lastIndexOf("."); + String officeType = fileName.substring(lastIndex+1); + fileName=fileName.substring(0,lastIndex); FileUtil.copyFile(file, tempPath, 0); - pdfFile = PdfConcurrenceUtil.convertToPdf(tempPath, tempPathSuccess); + if(Global.getPropertyToBoolean("open.office.isEnable", "true")) {//鍚敤oepn office杞崲 + pdfFile = PdfConcurrenceUtil.convertToPdf(tempPath, tempPathSuccess); + }else{ + pdfFile = new File(AsposeUtil.OfficeToPdf(tempPath, tempPathSuccess,officeType)); + } String s = Global.getSystemConfig("temp.dir", "") + "/" + "convertPDF_encryptTemp_" + random + ".pdf"; FileUtil.copyFile(pdfFile, s, isEncrypt ? 1 : 0); pdfFile.delete(); pdfFile = new File(s); uploadOtherFile(false, uploadServer, placeLocation, pdfFile, fileName); return true; - } catch (Exception e) { + } + catch (BaseException e) { + throw e; + }catch (Exception e) { e.printStackTrace(); throw new BaseException(FileCode.CONVERT_PDF_FILE_FAIL); } finally { try { - if (pdfFile.isFile()) { + if (pdfFile !=null && pdfFile.isFile()) { pdfFile.delete(); } com.product.common.io.FileUtils.delFile(tempPath); diff --git a/src/main/java/com/product/file/util/PdfConcurrenceUtil.java b/src/main/java/com/product/file/util/PdfConcurrenceUtil.java index 96436de..784fc7c 100644 --- a/src/main/java/com/product/file/util/PdfConcurrenceUtil.java +++ b/src/main/java/com/product/file/util/PdfConcurrenceUtil.java @@ -2,6 +2,7 @@ import com.alibaba.druid.util.StringUtils; import com.product.core.config.Global; +import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.file.config.FileCode; import org.artofsolving.jodconverter.OfficeDocumentConverter; @@ -45,6 +46,9 @@ * 鍒濆鍖栬浆鎹㈡湇鍔� */ public static boolean init() { + if(!Global.getPropertyToBoolean("open.office.isEnable", "true")) {//涓嶅惎鐢╫epn office杞崲 + return true; + } SpringMVCContextHolder.getSystemLogger().info("鍒濆鍖栬浆鎹㈡湇鍔�->"); openOfficeHome = Global.getSystemConfig("open.office.home",""); portInfo = Global.getSystemConfig("open.office.port.info",""); @@ -148,22 +152,20 @@ * @param output * @return */ - public static File convertToPdf(String input, String output) { + public static File convertToPdf(String input, String output) throws Exception { File inputFile = null; File outFile = null; OfficeManager officeManager = null; try { - SpringMVCContextHolder.getSystemLogger().info("浠庨槦鍒椾腑鎻愬彇杞崲绔彛..."); officeManager = officeManagerQueue.take(); inputFile = new File(input); outFile = new File(output); SpringMVCContextHolder.getSystemLogger().info("寮�濮嬭浆鎹㈡枃妗o細" + input + "=>" + output); OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); converter.convert(inputFile, outFile); // 杞崲鏂囨。 - } catch (Exception e) { - e.printStackTrace(); - SpringMVCContextHolder.getSystemLogger().error("杞崲鏂囨。鍑洪敊" + e); + }catch (Exception e) { outFile = null; + throw e; } finally { SpringMVCContextHolder.getSystemLogger().info("缁撴潫杞崲鏂囨。"); if (officeManager != null) { -- Gitblit v1.9.2