内に含まその他のドキュメントサポート リソース | PDF 文書ファイルをダウンロードする (626 KB)
Chapter 4 Sample CodeAs a web service, StarOffice 8 Server - PDF Converter is mainly triggered via programming. In the following, you will find two example ways to use the web service and an overview of the service parameters. Sample JSPA sample JSP file for a PDF conversion web application can be found in <socs-installation-dir>/engine/webapps/soserv/convert.jsp. This file is a template for own conversion JSPs. The sample JSP is fully working and can be accessed using a web browser. For example, for a StarOffice 8 Server - PDF Converter that runs locally and listens on port 8080, the respective URL is http://localhost:8080/soserv/convert.jsp. A screenshot of the page which is opened in the browser is given below. Figure 4–1 Screen shot of StarOffice 8 Server - PDF Converter in a web browser.
Use the Browse button to select a file to be converted, and click the Convert button to send the file to the server for conversion. After conversion, a dialog opens, where you can choose to download or open the converted file. The StarOffice 8 Server - PDF Converter browser page shows the following options:
Sample Java Web Service clientThe main way to use the web service is via custom clients, embedded in specialized applications. It does not make sense to discuss every particular language binding here, but shown is the way to obtain a connection to the web service using the JAX-WS implementation of Web Services provided by Sun Microsystems. The best way to create a JAX-WS client is to use Netbeans. Create a new file with the “Web Service Client” template in Netbeans with the provided wsdl. A client could look like the following. package soserverclient;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Holder;
public class ConversionClient {
String serviceURL="http://localhost:8080/soserv/SOConverter";
ConverterSEI converter = null;
ConvertMime params = null;
/** Creates a new instance of ConversionClient */
public ConversionClient(String serviceUrl) {
if (serviceUrl != null)
serviceURL = serviceUrl;
try {
converter = new Converter().getConverterSEIPort();
((BindingProvider)converter).getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
params = new ConvertMime();
params.setTargetType("PDF");
} catch (Throwable e) {
System.out.println("caught exception: " + e.getClass().getName());
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("usage: ConversionClient <inputdir> <outputdir> [<service url>]");
System.exit(-1);
}
String serviceUrl = null;
if (args.length > 2)
serviceUrl = args[2];
System.out.println("ConversionClient...");
ConversionClient dc = new ConversionClient(serviceUrl);
dc.convertDocs(args[0], args[1] );
}
public void convertDocs(String input, String output) {
File dir = new File(input);
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
String childurl
= dir.getAbsolutePath() + File.separator + children[i];
File f = new File(childurl);
if (f.isDirectory()) {
File o = new File(output + File.separatorChar + children[i]);
if (o.exists()) {
// TODO: error handling
} else {
o.mkdir();
}
convertDocs(
childurl, output + File.separatorChar + children[i]);
} else {
String name
= children[i].substring(
0, children[i].lastIndexOf('.')) + ".pdf";
File o = new File(output + File.separatorChar + name);
convertDoc(f, o);
}
}
}
public void convertDoc(File input, File output) {
try {
System.out.print(
"conversion of \"" + input.getAbsolutePath() + "\"... ");
byte[] fileStream = getByteArray( input );
Holder<byte[]> mimeAttachment = new Holder<byte[]>();
mimeAttachment.value = fileStream;
// Important: If possible, supply original source file extension
// with conversion request. Otherwiese, some input file formats
// will not be recognized properly and conversion will fail.
String options = createFileExtensionOption(input);
if (options != null)
params.setOptionString(options);
ConvertMimeResponse response
= converter.convertMime(params, mimeAttachment);
InputStream in = new ByteArrayInputStream(mimeAttachment.value);
FileOutputStream fout = new FileOutputStream(output);
int n = 0;
byte[] buf = new byte[4096];
while ((n = in.read(buf))!=-1)
fout.write(buf, 0, n);
fout.close();
String status = response.getStatus();
System.out.println("... done: status: " + status);
} catch (Throwable e) {
System.out.println("caught exception: " + e.getClass().getName());
e.printStackTrace();
}
}
private byte[] getByteArray(File file)
throws Exception {
ByteArrayOutputStream byteArrayOutputStream
= new ByteArrayOutputStream();
FileInputStream fin = new FileInputStream(file);
byte[] buffer = new byte[16384];
for (int len = fin.read(buffer); len > 0; len = fin.read(buffer)) {
byteArrayOutputStream.write(buffer, 0, len);
}
fin.close();
return byteArrayOutputStream.toByteArray();
}
private static String createFileExtensionOption(File srcFile) {
String ext = srcFile.getName();
int idx = ext.lastIndexOf('.');
if (idx >= 0) {
ext = ext.substring(idx+1);
if (!ext.equals("")) {
String options = "settings.sourceFileExtension=";
try {
options += URLEncoder.encode(ext, "UTF-8");
} catch (UnsupportedEncodingException e) {
options += ext;
}
return options;
}
}
return null;
}
}
|