Why file format conversion server?
I wonder if you have encountered such a situation. When you browse technical websites of companies such as IBM sun, you will find a small icon on the top of each page, you can download the current page content in word, PDF, or other formats. Maybe you are thinking, well, the practice of large companies is really formal, they provide many format versions for different users to download. I think you may be right, but we have another idea. Suppose we build a server, all formats can be freely converted into the desired format:
Suppose our Java code is written in this way.
Public static filetransfer {public static convert (File inputfile, file outputfile ){.........}} |
We can call this static method in JSP servlet to convert the document format.
2. How to ImplementWell, how can we implement the design idea? Fortunately, there are a lot of open-source communities in Java, and we have Google. In less than a minute, we can find the answer from the Internet: http://www.artofsolving.com/This is a solution implemented using OpenOffice, he has helped to do the whole solution.
JodconverterIs a subitem of this project, which is implemented in Java: Converting n Microsoft Office to OpenDocument and viceversa N word to OpenDocument text (ODT); N OpenDocument text (ODT) convert to word n EXCEL to OpenDocument spreadsheet (ODS); N OpenDocument spreadsheet (ODS) to Excel n PowerPoint to OpenDocument presentation (ODP); N OpenDocument presentation (ODP) convert to PowerPoint n any format into PDF n opendocument (text, spreadsheet, Presentation) into pdf n word to PDF; Convert EXCEL to PDF; convert PowerPoint to PDF n RTF to PDF; Convert WordPerfect to PDF ;... N also: N OpenDocument presentation (ODS) to flash; n PowerPoint to flash n RTF to Opendocument; WordPerfect to OpenDocument n any format to HTML (with limitations) n support for OpenOffice.org 1.0 and old StarOffice formats
3. Deploy the serviceThis document assumes that you have installed OpenOffice on the operating system. It is very simple and pleasant to install OpenOffice on windows. In Linux/Unix, the download package of openofficewebsite is in the tar.gz format, use gunzip-C ** .tar.gz tar-XF *. tarpkgadd-D/
Your pathThe/OOO/packages command can be installed to facilitate the use of different operating systems.
3.1. Windows 2 k XP 2003 Server1. download Windows Server 2003 Resource Kit tools2. use srvany.exe to create a service. For example, openofficeunoserver creates a subitem in the registry entry HKEY_LOCAL_MACHINE/system/controlset001/services/openofficeunoserver: parameters creates the string values application and appparameters under parameters, respectively set the values to C:/program files/OpenOffice.org 2.2/Program/soffice.exe-accept = socket, host = 0.0.0.0, Port = 8100; the path of soffice.exe in URP; -headless is modified according to the path you have installed to open the service in Control Panel/Administrative Tools/services, and the attribute log on account is changed to local service 3. modify share/Registry/data/org/OpenOffice/setup. find the xcu file:
<Prop oor: Name = "oosetupinstcompleted"> <value> false </value> </prop> <prop oor: name = "oosetupshowintro"> <value> true </value> </prop> |
Modify
<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. Start OpenOffice once in the program to set the registration option to not register 5. Start openofficeunoserver service 6. Check whether the service already has Telnet 127.0.0.1 8100
3.2. Linux/UnixLinux and UNIX are relatively simple to create services, but soffice needs to use the XWindow interface. Therefore, the command line status does not support graphical interfaces during service creation, therefore, you need to use Xvfb to set a virtual interface. Create an ooservice file in the soffice. bin directory # Touch ooservice # vi./ooservice write the following content into this file
#! /Sbin/shcase "$1" instart) 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; esacexit 0 |
The above code passes the test under Solaris 10. For other versions, adjust the parameters according to different Xvfb versions:/usr/openwin/bin/Xvfb: 5 screen 1024x768x24 & # chmod A + X. /ooservice in/etc/rc3.d or init. d. Create a file s90ooservice # Touch s90ooservice # vi s90ooservice and change the content
#! /Sbin/SH/usr/opt/OpenOffice. org2.2/Program/ooservice start |
# Chmod A + X./s90ooservice
4. Use instancesDownload: jodconverter instance code: <% @ page import = "Java. Io. File" %>
<% @ Page import = "com. artofsolving. jodconverter. OpenOffice. Connection. openofficeconnection" %>
<% @ Page import = "com. artofsolving. jodconverter. OpenOffice. Connection. socketopenofficeconnection" %>
<% @ Page import = "com. artofsolving. jodconverter. OpenOffice. converter. openofficedocumentconverter" %>
<% @ Page import = "com. artofsolving. jodconverter. documentconverter" %>
<% --
Created by intellij idea.
User: Henry
Date: 2007-7-27
Time: 15:30:43
To change this template use file | Settings | file templates.
-- %>
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" %>
<%
File inputfile = new file ("C: // temp // 111.ppt ");
File outputfile = new file ("C: // temp // 111.html ");
// Link an OpenOffice.org instance running on port 8100
Openofficeconnection connection = new socketopenofficeconnection (8100 );
Connection. Connect ();
// Create a converter object and convert the format
Documentconverter converter = new openofficedocumentconverter (connection );
Converter. Convert (inputfile, outputfile );
// Close the connection
Connection. Disconnect ();
%>
<HTML>
<Head> <title> simple JSP page </title> <Body> place your content here </body>
</Html>
5. Extended useIn the above Code, use onverter. convert (inputstream, documentformat, outputstream, documentformat); method, you can directly use the servlet outwriter object as the output stream, which can be used to convert files in the serverlet.
About the authorOldjavaman has been committed to technical work in Java-related fields for a long time and is mainly involved in the design of J2EE-related programs. He is currently working in a software company in Nanjing and hopes to make friends with a wide range of Java enthusiasts. You can contact him by mail.