规避mingganci

pull/1/head
liuwx_gitee 3 years ago
parent 612df609db
commit 812fa972fd

@ -38,7 +38,7 @@
版权声明 版权声明
----------------------------------- -----------------------------------
开源不代表免费,科亿遵循 GPL-3.0 开源协议发布,并提供技术交流学习,但 绝不允许修改后和衍生的代码做为闭源的商业软件发布和销售 如果需要将本产品在本地进行任何附带商业化性质行为使用, 请联系项目负责人进行商业授权 ,以遵守 GPL 协议保证您的正常使用。 开源不代表免费,科亿遵循 GPL-3.0 开源协议发布,并提供技术交流学习,但 绝不允许修改后和衍生的代码做为闭源的商业软件发布和销售 如果需要将本产品在本地进行任何附带商业化性质行为使用, 请联系项目负责人进行商业授权 ,以遵守 GPL 协议保证您的正常使用。
目前在国内 GPL 协议具备合同特征,是一种民事法律行为 属于我国《合同法》调整的范围。科亿项目团队保留一切诉讼权利。 科亿开源团队拥有对本开源协议的最终解释权。 目前在国内 GPL 协议具备合同特征,属于我国《合同法》调整的范围。科亿项目团队保留一切诉讼权利。 科亿开源团队拥有对本开源协议的最终解释权。
为什么选择KY KMS? 为什么选择KY KMS?

@ -2,90 +2,83 @@ package org.jeecg.modules.KM.common.utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.slf4j.LoggerFactory; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.File; import org.apache.poi.ss.usermodel.PrintSetup;
import java.io.IOException; import org.apache.poi.ss.usermodel.Sheet;
import java.io.InputStream; import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
/** /**
* *
* Title: OfficeUtils.java * Title: OfficeUtils.java
* *
* @author zxc * @author lwx
* @time 2018/6/29 5:20 * @time 2018/6/29 5:20
*/ */
@Slf4j @Slf4j
public class OfficeUtils { public class OfficeUtils {
// private static Logger log = LoggerFactory.getLogger(OfficeUtils.class); private static void formatExcel(String srcFile){
String fileExt = StringUtils.getFileSuffix(srcFile);
public static boolean convertPdf2(String sofficePath,File srcFile, File targetDir){ if ( fileExt != null ) {
///Applications/LibreOffice.app/Contents/MacOS/soffice
// --headless --invisible --convert-to
// html --outdir pdf-html/ *.pdf
Runtime runtime =Runtime.getRuntime();
StringBuilder command = new StringBuilder();
command.append(sofficePath);
String outputDir = targetDir.getAbsolutePath();
command.append(" --headless --invisible --convert-to pdf --outdir ");
command.append(outputDir);
command.append(" ");
command.append(srcFile.getAbsolutePath());
log.info("开始转换pdf,命令行是:{}",command.toString());
try { try {
if(fileExt.equals("xlsx")){
ProcessBuilder pb = new ProcessBuilder(command.toString()); XSSFWorkbook wb = new XSSFWorkbook(Files.newInputStream(Paths.get(srcFile)));
pb.redirectErrorStream(true);
Process process = pb.start(); for (int i = 0; i < wb.getNumberOfSheets(); i++) {
printStream(process.getInputStream()); XSSFSheet sheet = wb.getSheetAt(i);
//Process process = runtime.exec(command.toString()); //打印设置
// printStream(process.getInputStream()); XSSFPrintSetup print = sheet.getPrintSetup();
// printStream(process.getErrorStream()); print.setLandscape(true); // 打印方向true横向false纵向(默认)
int code = process.waitFor(); print.setFitHeight((short)0);//设置高度为自动分页
log.info("code:{}",code); print.setFitWidth((short)1);//设置宽度为一页
print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
//String result = IOUtils.toString(process.getInputStream(),"utf-8"); // print.setScale((short)55);//自定义缩放①此处100为无缩放
//String errorResult = IOUtils.toString(process.getErrorStream(),"utf-8"); //启用“适合页面”打印选项的标志
//log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); sheet.setFitToPage(true);
// if(StringUtil.isNotEmpty(errorResult)){ }
// log.error("convertPdf from {} output {}:\n errorResult:{}\n",srcFile.getAbsolutePath(),outputDir,errorResult); File file = new File(srcFile);
// } OutputStream fos = new FileOutputStream(file);
File targetFile = new File(targetDir,StringUtils.getFileNameWithoutSuffix(srcFile.getName())+".pdf"); wb.write(fos);
log.info("目标文件是:{}",targetFile.getAbsolutePath()); }else if(fileExt.equals("xls")){
if(targetFile.exists()){ POIFSFileSystem fs = new POIFSFileSystem(Files.newInputStream(Paths.get(srcFile)));
log.info("文档转化成功"); HSSFWorkbook wb = new HSSFWorkbook(fs);
return true;
}else { for (int i = 0; i < wb.getNumberOfSheets(); i++) {
log.info("文档转化失败"); Sheet sheet = wb.getSheetAt(i);
return false; //打印设置
PrintSetup print = sheet.getPrintSetup();
print.setLandscape(true); // 打印方向true横向false纵向(默认)
print.setFitHeight((short)0);//设置高度为自动分页
print.setFitWidth((short)1);//设置宽度为一页
print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
//启用“适合页面”打印选项的标志
sheet.setFitToPage(true);
}
File file = new File(srcFile);
OutputStream fos = new FileOutputStream(file);
wb.write(fos);
} }
// if(code==0){
// log.info("文档转化成功");
// return true;
// }else {
// log.info("文档转化失败");
// return false;
// }
} catch (IOException e) { } catch (IOException e) {
log.error("convertPdf",e); throw new RuntimeException(e);
} catch (InterruptedException e) { }
log.error("convertPdf",e);
} }
return false;
} }
public static boolean convertPdf(String sofficePath,File srcFile, File targetDir){ public static boolean convertPdf(String sofficePath,File srcFile, File targetDir){
///Applications/LibreOffice.app/Contents/MacOS/soffice //设置excel文件格式为不换行折断
// --headless --invisible --convert-to formatExcel(srcFile.getAbsolutePath());
// html --outdir pdf-html/ *.pdf
//Thread.sleep();
List<String>commands=new ArrayList<>(); List<String>commands=new ArrayList<>();
String outputDir = targetDir.getAbsolutePath(); String outputDir = targetDir.getAbsolutePath();
commands.add(sofficePath); commands.add(sofficePath);
@ -103,40 +96,31 @@ public class OfficeUtils {
pb.redirectErrorStream(true); pb.redirectErrorStream(true);
Process process = pb.start(); Process process = pb.start();
String result = IOUtils.toString(process.getInputStream(),"utf-8"); String result = IOUtils.toString(process.getInputStream(),"utf-8");
log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); boolean exitFlag = process.waitFor(10, TimeUnit.SECONDS);
//printStream(process.getInputStream()); if(exitFlag) {
//Process process = runtime.exec(command.toString()); int code = process.exitValue();
// printStream(process.getInputStream());
// printStream(process.getErrorStream()); log.info("convertPdf from {} output: {}\n result:{}\n exitValue:{}\n", srcFile.getAbsolutePath(), outputDir, result, code);
//int code = process.waitFor();
//log.info("code:{}",code);
process.destroy(); process.destroy();
//String result = IOUtils.toString(process.getInputStream(),"utf-8"); File targetFile = new File(targetDir, StringUtils.getFileNameWithoutSuffix(srcFile.getName()) + ".pdf");
//String errorResult = IOUtils.toString(process.getErrorStream(),"utf-8"); if (targetFile.exists()) {
//log.info("convertPdf from {} output {}:\n result:{}\n",srcFile.getAbsolutePath(),outputDir,result); log.info("文档转化成功,{},目标文件是:{}", srcFile.getAbsolutePath(), targetFile.getAbsolutePath());
// if(StringUtil.isNotEmpty(errorResult)){
// log.error("convertPdf from {} output {}:\n errorResult:{}\n",srcFile.getAbsolutePath(),outputDir,errorResult);
// }
File targetFile = new File(targetDir,StringUtils.getFileNameWithoutSuffix(srcFile.getName())+".pdf");
log.info("目标文件是:{}",targetFile.getAbsolutePath());
if(targetFile.exists()){
log.info("文档转化成功");
return true; return true;
}else { } else {
log.info("文档转化失败"); log.info("文档转化失败,{},目标文件是:{}", srcFile.getAbsolutePath(), targetFile.getAbsolutePath());
return false;
}
}
else {
log.info("等待命令退出失败");
return false; return false;
} }
// if(code==0){
// log.info("文档转化成功");
// return true;
// }else {
// log.info("文档转化失败");
// return false;
// }
} catch (IOException e) { } catch (IOException e) {
log.error("convertPdf",e); log.error("convertPdf IOException:",e);
}catch (InterruptedException e) {
log.error("convertPdf InterruptedException:",e);
} }
return false; return false;
} }
@ -156,6 +140,4 @@ public class OfficeUtils {
} }
}.start(); }.start();
} }
} }

Loading…
Cancel
Save