Java实现在线预览office
2014-08-26 16:22
337 查看
最近项目用到在线预览office相关文件。额、一想。貌似不难实现、java的poi可以实现、然后。。。发现事不如愿、针对office的格式多样化。poi显的力不从心、遂放弃折腾、由于服务器为linux!故有关使用 jacob-1.15-M4-x86.dll 来转换的方法也得淘汰。发现QQ邮箱、163邮箱等邮件中附件在线预览很是牛叉。经了解。原来他们使用国人的永中office来转换。效果非常好。不过设计付费、版权等问题。也不实际。所以另辟蹊径、找到基于openoffice的转换方式、兼容window与linux。开源免费、大块人心。
将Word转Html的原理是这样的:
1、客户上传Word文档到服务器
2、服务器调用OpenOffice程序打开上传的Word文档
3、OpenOffice将Word文档另存为Html格式
4、Over
关于openoffice的安装与服务的启动这里不做太多介绍了。下面附上利用openoffice将word转html的核心代码:
转http://aijava.cn/1939.html
而在前台展示可以使用html直接展示也可以使用openoffic转成pdf然后在用flexpaper(比如播放ppt)
将Word转Html的原理是这样的:
1、客户上传Word文档到服务器
2、服务器调用OpenOffice程序打开上传的Word文档
3、OpenOffice将Word文档另存为Html格式
4、Over
关于openoffice的安装与服务的启动这里不做太多介绍了。下面附上利用openoffice将word转html的核心代码:
package 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; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; import config.AppConfig; /** * 利用jodconverter(基于OpenOffice服务)将word文件(*.doc)转化为html格式, * 使用前请检查OpenOffice服务是否已经开启, * OpenOffice进程名称:soffice.exe | soffice.bin * @author linshutao * */ public class Doc2HtmlUtil { Log log = LogFactory.getLog(getClass()); private static Doc2HtmlUtil doc2HtmlUtil; /** * 获取Doc2HtmlUtil实例 * */ public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance(){ if(doc2HtmlUtil == null){ doc2HtmlUtil = new Doc2HtmlUtil(); } return doc2HtmlUtil; } /** * 转换文件 * @param fromFileInputStream: * */ public String doc2Html(InputStream 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 date = new Date(); SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmmss"); String timesuffix = sdf.format(date); String htmFileName = "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); /** * 由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) { log.error(e.getMessage(), e); } OpenOfficeConnection connection = new SocketOpenOfficeConnection(soffice_host,Integer.parseInt(soffice_port)); try { connection.connect(); } catch (ConnectException e) { System.err.println("文件转换出错,请检查OpenOffice服务是否启动。"); log.error(e.getMessage(), e); } // convert DocumentConverter 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();*/ //转换完之后删除word文件 docInputFile.delete(); log.debug("删除上传文件:"+docInputFile.getName()); return htmFileName; } }
转http://aijava.cn/1939.html
而在前台展示可以使用html直接展示也可以使用openoffic转成pdf然后在用flexpaper(比如播放ppt)
相关文章推荐
- Java实现在线预览附件 office转换PDF
- 在线预览-Java 使用 Print2Flash 实现Office文档在线阅读
- Java实现文档在线预览(openoffice+swfTools+FlexPaper)
- java实现office文件在线预览
- Office Web Apps Server作为Client,Java代码作为Host实现Office在线预览'
- Java实现office文档与pdf文档的在线预览功能
- java实现在线预览office文档
- Java实现文档在线预览(openoffice+swfTools+FlexPaper)
- Java实现office文档与pdf文档的在线预览功能
- Java实现web在线预览office文档与pdf文档实例
- Java 如何实现在线预览文档及修改(Office文件)
- Java实现office文档与pdf文档的在线预览功能
- java SpringMvc 实现文件在线预览(openoffice+swftools+flexpaper)
- Java实现Office在线预览
- java零碎要点010---Java实现office文档与pdf文档的在线预览功能
- java实现附件预览(openoffice+swftools+flexpaper)
- 基于微软office web apps实现在线预览文档
- Office在线预览及PDF在线预览的实现方式大集合
- Office在线预览及PDF在线预览的实现方式大集合
- 经管资源库项目总结----在线预览office文件的实现与总结