构建文件格式转换服务器
 
 构建文件格式转换服务器 
 
 
 1.   为什么要做文件格式转换服务器 
 
 
      不知道各位是否遇到这样的情况, 当您在浏览IBM SUN等公司的技术网站的时候, 你会发现在每个页面的上方有小图标, 你可以把当前的页面内容以word, pdf或者是其他的格式进行下载, 可能你在想, 恩, 大公司的做法确实很正规, 他们提供了许多的格式版本提供不同的用户下载, 我想你可能是对的, 但是我们换一种想法假设我们构建一个服务器, 让所有的格式可以自由地转换成我们所希望地格式: 
 
 
 假设我们的Java代码是这样写的 
 
|  
     Public static FileTransfer{ 
      
          Public static convert(File inputFile ,File outputFile){ 
      
           ……… 
      
     } 
      
     } 
     | 
 
 那么我们可以在Jsp Servlet里面调用这个静态方法, 实现文档的格式的转换。 
 
 
 2.   怎么实现 
 
 
    好了, 设计思想有了, 怎么实现呢, 好在Java有大量的开源社区,而且我们有google , 不出一分钟, 我们可以从互联网找到答案: 
 
 
 
 这个是一个利用Openoffice来实现的解决方案, 他已经帮忙做好了整个解决方案。 
 
 
 JODConverter 
 是这个项目的子项, 
 用 
 Java 
 语言来实现: 
 
 
 n             
 Microsoft Office 转换成OpenDocument和viceversa  
 
 
 n             
 Word 格式转换成 OpenDocument Text (odt); 
 
 
 n             
 OpenDocument Text (odt) 转换成 Word  
 
 
 n             
 Excel转换成OpenDocument Spreadsheet (ods);  
 
 
 n             
 OpenDocument Spreadsheet (ods) 转换成Excel  
 
 
 n             
 PowerPoint转换成OpenDocument Presentation (odp); 
 
 
 n             
 OpenDocument Presentation (odp) 转换成PowerPoint  
 
 
 n             
 任何格式转换成 PDF  
 
 
 n             
 OpenDocument (Text, Spreadsheet, Presentation) 转换成PDF  
 
 
 n             
 Word to PDF; Excel转换成PDF; PowerPoint转换成PDF  
 
 
 n             
 RTF转换成PDF; WordPerfect转换成PDF; ...  
 
 
 n             
 还有: 
 
 
 n             
 OpenDocument Presentation (ods) 转换成Flash;  
 
 
 n             
 PowerPoint转换成Flash  
 
 
 n             
 RTF转换成OpenDocument; WordPerfect转换成OpenDocument  
 
 
 n             
 任何格式转化成 HTML (with limitations)  
 
 
 n             
 Support for OpenOffice.org 1.0 and old StarOffice formats 
 
 
 3.   部署服务 
 
 
 这个文档假设你已经安装了OpenOffice在操作系统上, 在windows上安装OpenOffice是非常简单和愉快的事情, 在linux/unix下面, OpenOffice网站提供的下载包是Tar.gz格式, 用 
 
 
 Gunzip –c **.tar.gz 
 
 
 Tar –xf *.tar 
 
 
 Pkgadd -d /your path/OOo/packages  
 
 
 命令就可以安装  
 
 
 为了方便使用我按照不同的操作系统来编写 
 
 
 3.1.           Windows 2k xp 2003 server 
 
 
 
 2.          
 利用Srvany.exe创建一个服务, 如OpenOfficeUnoServer 
 
 
 在注册表项 : 
 
 
  HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001/Services/OpenOfficeUnoServer 
 
 
 创建子项: Parameters  
 
 
 在parameters下面 创建字符串值 Application 和 AppParameters,  
 
 
 分别设置值为 
 
 
 C:/Program Files/OpenOffice.org 2.2/program/soffice.exe 
 
 
 -accept=socket,host=0.0.0.0,port=8100;urp; -headless 
 
 
 其中soffice.exe的路径根据您安装的路径来修改 
 
 
 在Control Panel / Administrative Tools / Services 打开该服务, 修改属性 Log On account 为  Local Service  
 
 
 3.          
 修改您安装OpenOffice路径下的share/registry/data/org/openoffice/Setup.xcu文件 
 
 
 找到: 
 
|  
     <prop oor:name="ooSetupInstCompleted"> 
      
      <value>false</value> 
      
     </prop> 
      
     <prop oor:name="ooSetupShowIntro"> 
      
      <value>true</value> 
      
     </prop> 
     | 
 
 修改成 
 
|  
     <prop oor:name="ooSetupInstCompleted" oor:type="xs:boolean"> 
      
      <value>true</value> 
      
     </prop> 
      
     <prop oor:name="LicenseAcceptDate" oor:type="xs:string"> 
      
      <value>2006-07-25T17:34:04</value> 
      
     </prop> 
      
     <prop oor:name="FirstStartWizardCompleted" oor:type="xs:boolean"> 
      
      <value>true</value> 
      
     </prop> 
     | 
 
 4.         从开始, 程序里面启动一次OpenOffice 将注册选项设置成不注册 
   
 
 
 5.          
 启动OpenOfficeUnoServer服务 
 
 
 6.         查看是否服务已经存在 
 telnet 127.0.0.1 8100 
 
 
 3.2.           Linux / Unix 
 
 
 Linux和Unix创建服务相对简单, 但是由于soffice需要使用到Xwindow界面, 所以在做服务的时候, 由于在命令行状态, 没有图形界面的支持, 所以需要使用Xvfb来设置一个虚拟的界面。 
 
 
 在soffice.bin目录创建一个ooService文件 
 
 
 # touch ooService 
 
 
 # vi ./ooService 
 
 
 将以下内容写入这个文件 
 
|  
     #!/sbin/sh 
      
     case "$1" in 
      
     start) 
      
          DISPLAY=:5.0 
      
          export DISPLAY 
      
          /usr/openwin/bin/Xvfb :5 screen 1024x768x24 &  
      
             /usr/opt/openoffice.org2.2/program/soffice.bin -headless -display:5 -accept="socket,host=0.0.0.0,port=8100;urp;" & 
      
            ;; 
      
     stop) 
      
            pkill soffice 
      
            ;; 
      
     *) 
      
            echo "Usage: $0 { start | stop }" 
      
            exit 1 
      
            ;; 
      
     esac 
      
     exit 0 
     | 
 
 以上代码在solaris 10 
 下面测试通过, 其他的版本, 根据Xvfb的版本不一 , 进行调整参数即可:/usr/openwin/bin/Xvfb :5 screen 1024x768x24  & 
 
 
 # chmod a+x ./ooService 
 
 
 在/etc/rc3.d 或者init.d里面创建一个文件 S90ooService 
 
 
 # touch S90ooService 
 
 
 # vi S90ooService 
 
 
 将里面的内容改成 
 
|  
     #!/sbin/sh 
      
     /usr/opt/openoffice.org2.2/program/ooservice start 
     | 
 
 #chmod a+x ./S90ooService 
 
 
 4.   使用实例 
 
 
 下载:jodconverter 
 
 
 实例代码如下 
 
 <% 
   @ page  
   import 
   = 
   " 
   java.io.File 
   " 
   %>
 
   <% 
   @ page  
   import 
   = 
   " 
   java.io.File 
   " 
   %> 
    <% 
   @ page  
   import 
   = 
   " 
   com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection 
   " 
   %>
 
   <% 
   @ page  
   import 
   = 
   " 
   com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection 
   " 
   %> 
    <% 
   @ page  
   import 
   = 
   " 
   com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection 
   " 
   %>
 
   <% 
   @ page  
   import 
   = 
   " 
   com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection 
   " 
   %> 
    <% 
   @ page  
   import 
   = 
   " 
   com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter 
   " 
   %>
 
   <% 
   @ page  
   import 
   = 
   " 
   com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter 
   " 
   %> 
    <% 
   @ page  
   import 
   = 
   " 
   com.artofsolving.jodconverter.DocumentConverter 
   " 
   %>
 
   <% 
   @ page  
   import 
   = 
   " 
   com.artofsolving.jodconverter.DocumentConverter 
   " 
   %> 
    <%--
 
   <%-- 
    Created by IntelliJ IDEA.
 Created by IntelliJ IDEA. User: Henry
 User: Henry Date:  
   2007 
   - 
   7 
   - 
   27
 Date:  
   2007 
   - 
   7 
   - 
   27 
    Time:  
   15 
   : 
   30 
   : 
   43
 Time:  
   15 
   : 
   30 
   : 
   43 
    To change  
   this 
    template use File  
   | 
    Settings  
   | 
    File Templates.
 To change  
   this 
    template use File  
   | 
    Settings  
   | 
    File Templates. --%>
 
   --%> 
    <% 
   @ page contentType 
   = 
   " 
   text/html;charset=UTF-8 
   " 
    language 
   = 
   " 
   java 
   " 
     
   %>
 
   <% 
   @ page contentType 
   = 
   " 
   text/html;charset=UTF-8 
   " 
    language 
   = 
   " 
   java 
   " 
     
   %> 
    <%
 
   <% 
    File inputFile  
   = 
     
   new 
    File( 
   " 
   c://temp//111.ppt 
   " 
   );
 File inputFile  
   = 
     
   new 
    File( 
   " 
   c://temp//111.ppt 
   " 
   ); File outputFile  
   = 
     
   new 
    File( 
   " 
   c://temp//111.html 
   " 
   );
    File outputFile  
   = 
     
   new 
    File( 
   " 
   c://temp//111.html 
   " 
   ); 
  // 
    链接 一个运行在8100端口的OpenOffice.org 实例
     
   // 
    链接 一个运行在8100端口的OpenOffice.org 实例 
    OpenOfficeConnection connection  
   = 
     
   new 
    SocketOpenOfficeConnection( 
   8100 
   );
 
       OpenOfficeConnection connection  
   = 
     
   new 
    SocketOpenOfficeConnection( 
   8100 
   ); connection.connect();
    connection.connect(); 
  // 
    创建一个converter对象并转换格式
     
   // 
    创建一个converter对象并转换格式 
    DocumentConverter converter  
   = 
     
   new 
    OpenOfficeDocumentConverter(connection);
 
       DocumentConverter converter  
   = 
     
   new 
    OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile);
    converter.convert(inputFile, outputFile); 
  // 
    关闭连接
     
   // 
    关闭连接 
    connection.disconnect();
 
       connection.disconnect(); %>
 
   %> 
    
  
  < 
   html 
   >
 
   < 
   html 
   > 
    < 
   head 
   >< 
   title 
   > 
   Simple jsp page 
   </ 
   title 
   ></ 
   head 
   >
  
   < 
   head 
   >< 
   title 
   > 
   Simple jsp page 
   </ 
   title 
   ></ 
   head 
   > 
    < 
   body 
   > 
   Place your content here 
   </ 
   body 
   >
  
   < 
   body 
   > 
   Place your content here 
   </ 
   body 
   > 
    </ 
   html 
   >
 
   </ 
   html 
   > 
   
 5.   扩展使用 
 
 
 在上述代码里, 使用onverter.convert(inputStream , DocumentFormat, outputStream , DocumentFormat);方法, 可以直接将servlet的outwriter对象作为输出流, 既可以实现在serverlet里面转换文件了。 
 
 
  关于作者 
 
 
  
  OldJavaMan,长期致力于Java相关领域的技术工作, 主要参与J2EE相关程序的设计, 目前在南京的一家软件企业就职,他希望和广大的Java爱好者结交朋友。大家可以通过 
  mail 
  联系他 。