Using OpenOffice to implement a preview of a file using a stream

Source: Internet
Author: User
Tags flush socket

Some time ago, the project needs to do a file preview of the function by checking the data to complete, here to record

First we use the OpenOffice service to download the installation OpenOffice into the installation OpenOffice program directory to open the Command window. Execution

Soffice-headless-accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;"

command to start the service

Tool classes for converting PDFs

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.OpenOfficeDocumentConverter;
Import java.io.*;
Import java.net.ConnectException;
Import Java.net.URL;
Import Java.text.SimpleDateFormat;

Import Java.util.Date; /** * Created by Administrator * on 2018/3/12 */public class Doc2htmlutil {private static Doc2htmlutil Doc2htmlut

    Il /** * Get doc2htmlutil instance */public static synchronized Doc2htmlutil getdoc2htmlutilinstance () {if
        C2htmlutil = = null) {Doc2htmlutil = new doc2htmlutil ();
    } return doc2htmlutil;  }/** * Convert files into HTML * *@paramFromfileinputstream: *@throwsIOException */public string file2html (InputStream fromfileinputstream, string tofilepath,string type,string host
        , int port) 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 input file by Fromfileinputstream */try {outputstream OS = new FileOutputStream (doc
            Inputfile);
            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) {e.printstacktrace (); } openofficeconnection connection = new Socketopenofficeconnection (host,Port);
        try {connection.connect (); } catch (Connectexception e) {System.err.println ("File conversion error, please check if the OpenOffice service is started.
        ");
        }//Convert Documentconverter converter = new Openofficedocumentconverter (connection);
        Converter.convert (Docinputfile, htmloutputfile);
        Connection.disconnect ();
        Delete Word file after conversion docinputfile.delete ();
    return htmfilename;  }/** * Convert files into PDF * *@paramFromfileinputstream: *@throwsIOException */public string file2pdf (InputStream fromfileinputstream, string tofilepath,string type,string host,
        int port) 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 input file by Fromfileinputstream */try {outputstream OS = new FileOutputStream (doc
            Inputfile);
            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) {e.printstacktrace (); } openofficeconnection connection = new Socketopenofficeconnection (host,port);
        try {connection.connect (); } catch (Connectexception e) {System.err.println ("File conversion error, please check if the OpenOffice service is started.
        ");
        }//Convert Documentconverter converter = new Openofficedocumentconverter (connection);
        Converter.convert (Docinputfile, htmloutputfile);
        Connection.disconnect ();
        Delete Word file after conversion docinputfile.delete ();
    return htmfilename; }/** * Before executing, please start OpenOffice service * Go to $oo_home\program * Execute soffice-headless-accept= "socket,host=127.0.0.
     1,port=8100;urp; "  *@paramXlsfile *@paramTargetFile *@throwsException */public static void Fileconvertpdf (String xlsfile, String targetfile,string type,string host,int Port
        ) throws Exception {file XLSF = new File (xlsfile);
        File TARGETF = new file (targetfile);
        Get file format defaultdocumentformatregistry Formatreg = new Defaultdocumentformatregistry ();
        Documentformat Pdfformat = formatreg.getformatbyfileextension ("PDF");
        Documentformat Docformat = null; if ("Doc". Equals (type) | |
        "Docx". Equals (type) {Docformat = Formatreg.getformatbyfileextension ("Doc"); }else if ("xls". Equals (type) | |
        "Xlsx". Equals (Type)) {Docformat = formatreg.getformatbyfileextension ("xls");
        }else if ("ppt". Equals (Type)) {Docformat = Formatreg.getformatbyfileextension ("ppt");
        }else{Docformat = formatreg.getformatbyfileextension ("Doc");
      }//Stream in the form of inputstream InputStream = new FileInputStream (XLSF);  OutputStream outputstream = new FileOutputStream (TARGETF);
        /** * */openofficeconnection connection = new socketopenofficeconnection (8100);
            try {connection.connect ();
            Documentconverter converter = new Openofficedocumentconverter (connection);
        Converter.convert (InputStream, Docformat, OutputStream, Pdfformat);
        } catch (Connectexception e) {e.printstacktrace ();
                } finally {if (connection! = null) {connection.disconnect ();
            connection = null; }}}/** * Before executing, please start OpenOffice service * Go to $oo_home\program * Execute soffice-headless-accept= "Socke  T,host=127.0.0.1,port=8100;urp; "  or soffice-headless-accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;"-nofirststartwizard *@paramInputStream *@paramOutputStream *@throwsException */public static void Fileconvertpdf (InputStream inputstream, OutputStream outputstream,string type,str ing Host,int port) throws Exception {//Get file format defaultdocumentformatregistry Formatreg = new
        Defaultdocumentformatregistry ();
        Documentformat Pdfformat = formatreg.getformatbyfileextension ("PDF");
        Documentformat Docformat = null; if (". doc". Equals (type) | |
        ". docx". Equals (type) {Docformat = Formatreg.getformatbyfileextension ("Doc"); }else if (". xls". Equals (type) | |
        ". xlsx". Equals (Type)) {Docformat = formatreg.getformatbyfileextension ("xls");
        }else if (". ppt". Equals (Type)) {Docformat = Formatreg.getformatbyfileextension ("ppt");
        }else if (". txt". Equals (Type)) {Docformat = formatreg.getformatbyfileextension ("txt");
        }else if (". pdf". Equals (Type)) {Docformat = Formatreg.getformatbyfileextension ("PDF"); }else{DOCFORmat = Formatreg.getformatbyfileextension ("Doc");
        }//Stream in the form of openofficeconnection connection = new Socketopenofficeconnection (host,port);
            try {connection.connect ();
            Documentconverter converter = new Openofficedocumentconverter (connection);
        Converter.convert (InputStream, Docformat, OutputStream, Pdfformat);
        } catch (Connectexception e) {e.printstacktrace ();
                } finally {if (connection! = null) {connection.disconnect ();
            connection = null; }}} public static void Main (string[] args) throws Exception {URL url=new url ("http://192.168


        .6.152:9000/group1/m00/00/0a/wkggmfqafkkab6ixaaayafwbzcu610.xls ");//default home page InputStream is=url.openstream ();//Get network stream
        /*//Get network resources, encoding format will appear garbled **************** byte[] flush=new byte[1024];
        int len=0;
while ( -1!= (Len=is.read (flush))) {
            System.out.println (New String (Flush,0,len));
        } is.close (); Get network resources, encoding format will appear garbled *****************///solution garbled method, conversion flow <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.