Introduction
I have written an article about poi online preview, which also said that using openOffice can also be done. I will introduce it in detail here.
I have two implementation logic:
1. Use jodconverter (based on OpenOffice service) to convert files (.doc, .docx, .xls, .ppt) into html format.
2. Use jodconverter (based on OpenOffice service) to convert files (.doc, .docx, .xls, .ppt) into pdf format.
Everyone can understand the conversion to html format, so that you can view it directly on the browser, which realizes the online preview function; converting to the pdf format requires the user to install Adobe Reader XI, so you will find that dragging the pdf directly to the browser page can directly open the preview, which also realizes the online preview function.
Convert files to html format or pdf format
Without further ado, just upload the code.
package com.pdfPreview.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ConnectException;import java.text.SimpleDateFormat;import java.util.Date;import com.artofsolving.jodconverter.DocumentConverter;import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;/** * Use jodconverter (based on OpenOffice service) to convert files (*.doc, *.docx, *.xls, *.ppt) into html format or pdf format. * Before using, please check whether the OpenOffice service has been enabled. OpenOffice process name: software.exe | software.bin * * @author yjclsx */public class Doc2HtmlUtil { private static Doc2HtmlUtil doc2HtmlUtil; /** * Get Doc2HtmlUtil instance*/ public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() { if (doc2HtmlUtil == null) { doc2HtmlUtil = new Doc2HtmlUtil(); } return doc2HtmlUtil; } /** * Convert file to html * * @param fromFileInputStream: * @throws IOException */ public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String timesuffix = sdf.format(date); String docFileName = null; String htmFileName = null; if("doc".equals(type)){ docFileName = "doc_" + timesuffix + ".doc"; htmFileName = "doc_" + timesuffix + ".html"; }else if("docx".equals(type)){ docFileName = "docx_" + timesuffix + ".docx"; htmFileName = "docx_" + timesuffix + ".html"; }else if("xls".equals(type)){ docFileName = "xls_" + timesuffix + ".xls"; htmFileName = "xls_" + timesuffix + ".html"; }else if("ppt".equals(type)){ docFileName = "ppt_" + timesuffix + ".ppt"; htmFileName = "ppt_" + timesuffix + ".html"; }else{ return null; } File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName); File docInputFile = new File(toFilePath + File.separatorChar + docFileName); if (htmlOutputFile.exists()) htmlOutputFile.delete(); htmlOutputFile.createNewFile(); if (docInputFile.exists()) docInputFile.delete(); docInputFile.createNewFile(); /** * Build the input file fromFileInputStream*/ try { OutputStream os = new FileOutputStream(docInputFile); int bytesRead = 0; byte[] buffer = new byte[1024 * 8]; while ((bytesRead = fromFileInputStream.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fromFileInputStream.close(); } catch (IOException e) { } OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); } catch (ConnectException e) { System.err.println("There is an error in the file conversion, please check whether the OpenOffice service is started."); } // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docInputFile, htmlOutputFile); connection.disconnect(); // Delete the word file after conversion docInputFile.delete(); return htmFileName; } /** * Convert the file to pdf * * @param fromFileInputStream: * @throws IOException */ public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String timesuffix = sdf.format(date); String docFileName = null; String htmFileName = null; if("doc".equals(type)){ docFileName = "doc_" + timesuffix + ".doc"; htmFileName = "doc_" + timesuffix + ".pdf"; }else if("docx".equals(type)){ docFileName = "docx_" + timesuffix + ".docx"; htmFileName = "docx_" + timesuffix + ".pdf"; }else if("xls".equals(type)){ docFileName = "xls_" + timesuffix + ".xls"; htmFileName = "xls_" + timesuffix + ".pdf"; }else if("ppt".equals(type)){ docFileName = "ppt_" + timesuffix + ".ppt"; htmFileName = "ppt_" + timesuffix + ".pdf"; }else{ return null; } File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName); File docInputFile = new File(toFilePath + File.separatorChar + docFileName); if (htmlOutputFile.exists()) htmlOutputFile.delete(); htmlOutputFile.createNewFile(); if (docInputFile.exists()) docInputFile.delete(); docInputFile.createNewFile(); /** * Build the input file fromFileInputStream*/ try { OutputStream os = new FileOutputStream(docInputFile); int bytesRead = 0; byte[] buffer = new byte[1024 * 8]; while ((bytesRead = fromFileInputStream.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fromFileInputStream.close(); } catch (IOException e) { } OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); } catch (ConnectException e) { System.err.println("There is an error in the file conversion, please check whether the OpenOffice service is started."); } // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docInputFile, htmlOutputFile); connection.disconnect(); // Delete the word file after conversion docInputFile.delete(); return htmFileName; } public static void main(String[] args) throws IOException { Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance(); File file = null; FileInputStream fileInputStream = null; file = new File("D:/poi-test/exportExcel.xls"); fileInputStream = new FileInputStream(file);// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls"); coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls"); file = new File("D:/poi-test/test.doc"); fileInputStream = new FileInputStream(file);// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc","doc"); coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/doc","doc"); file = new File("D:/poi-test/weekly report template.ppt"); fileInputStream = new FileInputStream(file);// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt"); coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt"); file = new File("D:/poi-test/test.docx"); fileInputStream = new FileInputStream(file);// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx"); coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx"); }} The process of converting to html is almost the same as converting to pdf. The first one is named XXX.html when creating the output File, and the latter is named XXX.pdf. When executing converter.convert(docInputFile, htmlOutputFile);, jodconverter will convert it into the corresponding file according to the file type name.
Note that if both file2Html and file2pdf are called in the main method, an error will be reported. Either convert html or pdf, and you can only choose one. Also, before execution, you need to start the service of openOffice: execute software -headless -accept=”socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard in the command window in the openOffice directory to start.
The above needs to introduce jodconverter's jar package. I hope it will be helpful to everyone's learning, and I hope everyone will support Wulin.com more.