|
@@ -0,0 +1,257 @@
|
|
|
+package com.singularity.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 org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.ConnectException;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文件格式转换工具类
|
|
|
+ */
|
|
|
+public class FileConvertUtil {
|
|
|
+ /**
|
|
|
+ * 默认转换后文件后缀
|
|
|
+ */
|
|
|
+ private static final String DEFAULT_SUFFIX = "pdf";
|
|
|
+ /**
|
|
|
+ * openoffice的host:你部署openoffice的服务器ip
|
|
|
+ */
|
|
|
+ private static final String OPENOFFICE_HOST = "10.148.111.13";
|
|
|
+ /**
|
|
|
+ * openoffice的port
|
|
|
+ */
|
|
|
+ private static final Integer OPENOFFICE_PORT = 8100;
|
|
|
+
|
|
|
+ @Value("${file.path}")
|
|
|
+ private String filePath;
|
|
|
+
|
|
|
+ public static void test(){
|
|
|
+ OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_HOST, OPENOFFICE_PORT);
|
|
|
+ try {
|
|
|
+ connection.connect();
|
|
|
+ } catch (ConnectException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 方法描述 office文档转换为PDF(处理本地文件)
|
|
|
+ * @param sourcePath 源文件路径
|
|
|
+ * @param suffix 源文件后缀
|
|
|
+ * @return InputStream 转换后文件输入流
|
|
|
+ */
|
|
|
+ public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {
|
|
|
+ File inputFile = new File(sourcePath);
|
|
|
+ InputStream inputStream = new FileInputStream(inputFile);
|
|
|
+ return covertCommonByStream(inputStream, suffix);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * office文档转换为PDF文件流(处理网络文件)
|
|
|
+ *
|
|
|
+ * @param netFileUrl 网络文件路径
|
|
|
+ * @param suffix 文件后缀
|
|
|
+ * @return InputStream 转换后文件输入流
|
|
|
+ */
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * office文档转换为PDF文件(处理网络文件)
|
|
|
+ *
|
|
|
+ * @param netFileUrl 网络文件路径
|
|
|
+ * @param suffix 文件后缀
|
|
|
+ * @param targetPath 目标文件全路径 eg: C:\Users\cvec2022\Desktop\abc.pdf
|
|
|
+ * @return InputStream 转换后文件输入流
|
|
|
+ */
|
|
|
+ public static File convertNetFileToFile(String netFileUrl, String suffix, String targetPath) 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, targetPath);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将文件以流的形式转换
|
|
|
+ *
|
|
|
+ * @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_HOST, 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将文件以文件的形式转换
|
|
|
+ *
|
|
|
+ * @param inputStream 源文件输入流
|
|
|
+ * @param suffix 源文件后缀
|
|
|
+ * @param targetPath 目标文件全路径 eg: C:\Users\cvec2022\Desktop\abc.pdf
|
|
|
+ * @return File 转换后文件
|
|
|
+ */
|
|
|
+ public static File covertCommonByStream(InputStream inputStream, String suffix, String targetPath) throws Exception {
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
+ OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_HOST, 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();
|
|
|
+ ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
|
|
|
+ return byteArrayToFile(baos.toByteArray(), targetPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * byte数组转File
|
|
|
+ * @param byteArray 字节数组
|
|
|
+ * @param targetPath 目标路径
|
|
|
+ */
|
|
|
+ public static File byteArrayToFile(byte[] byteArray, String targetPath) {
|
|
|
+ InputStream in = new ByteArrayInputStream(byteArray);
|
|
|
+ File file = new File(targetPath);
|
|
|
+ String path = targetPath.substring(0, targetPath.lastIndexOf(File.separator));
|
|
|
+ if (!file.exists()) {
|
|
|
+ new File(path).mkdir();
|
|
|
+ }
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ try {
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
+ int len = 0;
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ while ((len = in.read(buf)) != -1) {
|
|
|
+ fos.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ fos.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (null != fos) {
|
|
|
+ try {
|
|
|
+ fos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * inputStream转File
|
|
|
+ */
|
|
|
+ public static void inputStreamToFile(InputStream ins, File file) throws IOException {
|
|
|
+ OutputStream os = Files.newOutputStream(file.toPath());
|
|
|
+ int bytesRead = 0;
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ os.close();
|
|
|
+ ins.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * outputStream转inputStream
|
|
|
+ */
|
|
|
+ public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {
|
|
|
+ ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
|
|
|
+ return new ByteArrayInputStream(baos.toByteArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * excel超范围预处理
|
|
|
+ */
|
|
|
+ public void refresh(String oldFilePath, String oldType, String newFilePath) throws IOException {
|
|
|
+ Workbook workbook = null;
|
|
|
+ if (oldType.equals("xls")) {
|
|
|
+ workbook = new HSSFWorkbook(new FileInputStream(oldFilePath));
|
|
|
+ } else if (oldType.equals("xlsx")) {
|
|
|
+ workbook = new XSSFWorkbook(new FileInputStream(oldFilePath));
|
|
|
+ }
|
|
|
+ assert workbook != null;
|
|
|
+ int sheetCount = workbook.getNumberOfSheets();
|
|
|
+ for (int i = 0; i < sheetCount; i++) {
|
|
|
+ Sheet sheet = workbook.getSheetAt(i);
|
|
|
+ //通过此处即可将excel中所有列放置在一页显示(打印),转换至pdf后也将在一页显示
|
|
|
+ XSSFPrintSetup printSetup = (XSSFPrintSetup) sheet.getPrintSetup();
|
|
|
+ printSetup.setFitHeight((short) 0);
|
|
|
+ sheet.setFitToPage(true);
|
|
|
+ }
|
|
|
+ workbook.write(new FileOutputStream(newFilePath));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * excel超范围预处理
|
|
|
+ */
|
|
|
+ public InputStream refresh(InputStream oldInputSteam, String oldType) throws IOException {
|
|
|
+ Workbook workbook = null;
|
|
|
+ if (oldType.equals("xls")) {
|
|
|
+ workbook = new HSSFWorkbook(oldInputSteam);
|
|
|
+ } else if (oldType.equals("xlsx")) {
|
|
|
+ workbook = new XSSFWorkbook(oldInputSteam);
|
|
|
+ }
|
|
|
+ assert workbook != null;
|
|
|
+ int sheetCount = workbook.getNumberOfSheets();
|
|
|
+ for (int i = 0; i < sheetCount; i++) {
|
|
|
+ Sheet sheet = workbook.getSheetAt(i);
|
|
|
+ sheet.setHorizontallyCenter(true);//设置打印页面为水平居中
|
|
|
+ sheet.getPrintSetup().setFitHeight((short) 0);
|
|
|
+ sheet.setFitToPage(true);
|
|
|
+ }
|
|
|
+ String uuid = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ String newFilePath = filePath + uuid + "changedFile.xlsx";
|
|
|
+ workbook.write(new FileOutputStream(newFilePath));
|
|
|
+ return new FileInputStream(newFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ convertNetFileToFile("https://file.test.com/test/20230320/88af234acc864bfb91de13c16b0469f8.docx", "docx", "C:\\Users\\cvec2022\\Desktop\\abc.pdf");
|
|
|
+ }
|
|
|
+}
|