Use jodconverter (based on OpenOffice service) to convert Word files (*. Doc) to HTML Format

Source: Internet
Author: User
Package COM. syni. im800.kb. common. util; import Java. io. file; 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 Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import COM. artofsolving. jodconverter. documentconverter; Im Port COM. artofsolving. jodconverter. openOffice. connection. openofficeconnection; import COM. artofsolving. jodconverter. openOffice. connection. socketopenofficeconnection; import COM. artofsolving. jodconverter. openOffice. converter. openofficedocumentconverter; import COM. syni. im800.kb. config. appconfig;/*** use jodconverter (based on the OpenOffice service) to convert the Word file (*. doc) is converted to HTML format. * check whether the OpenOffice service is enabled before use. * OpenOffice process name: So Ffice.exe | soffice. bin * @ author linshutao **/public class doc2htmlutil {log = logfactory. getlog (getclass (); Private Static doc2htmlutil doc2htmlutil;/*** get doc2htmlutil instance **/public static synchronized doc2htmlutil Merge () {If (doc2htmlutil = NULL) {doc2htmlutil = new doc2htmlutil ();} return doc2htmlutil;}/*** conversion file * @ Param fromfileinputstream: **/Public String doc2html (in Putstream fromfileinputstream, file tofilefolder) {string soffice_host = appconfig. getproperty (appconfig. soffice_host_key); string soffice_port = appconfig. getproperty (appconfig. soffice_port_key); log. debug ("soffice_host:" + soffice_host + ", soffice_port:" + soffice_port); Date = new date (); simpledateformat SDF = new simpledateformat ("yyyymmddhhmmss "); string timesuffix = SDF. format (date); string htmfilen Ame = "htmlfile" + timesuffix + ". html "; string docfilename =" docfile "+ timesuffix + ". doc "; file htmloutputfile = new file (tofilefolder. tostring () + file. separatorchar + htmfilename); file docinputfile = new file (tofilefolder. tostring () + file. separatorchar + docfilename); log. debug ("######## htmloutputfile:" + tofilefolder. tostring () + file. pathseparator + htmfilename);/*** build the input file by fromfileinputstream **/try {outputstr Eam 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) {log. error (E. getmessage (), e);} openofficeconnection connection = new socketopenofficeconnection (soffice_host, integer. parseint (soffice_port); try {connection. connect ();} catch (connectexception e) {system. err. println ("file conversion error. Check whether the OpenOffice service is enabled. "); Log. error (E. getmessage (), e);} // convertdocumentconverter converter = new openofficedocumentconverter (connection); converter. convert (docinputfile, htmloutputfile); connection. disconnect ();/* file htmloutputfile_rn = new file (htmloutputfile. getabsolutepath (). substring (0, htmloutputfile. getabsolutepath (). lastindexof (". ") + ". htm "); htmloutputfile. renameto (htmloutputfile_rn); Return htmloutputfile_rn.getname (); * // After conversion, delete the word file docinputfile. delete (); log. debug ("delete Upload File:" + docinputfile. getname (); Return htmfilename ;}}

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.