java 应用中使用 jodconvert
2018-10-23 本文已影响0人
金刚_30bf
版本: 4.2.0
示例代码:
File inputFile = new File("document.doc");
File outputFile = new File("document.pdf");
// Create an office manager using the default configuration.
// The default port is 2002. Note that when an office manager
// is installed, it will be the one used by default when
// a converter is created.
final LocalOfficeManager officeManager = LocalOfficeManager.install();
try {
// Start an office process and connect to the started instance (on port 2002).
officeManager.start();
// Convert
JodConverter
.convert(inputFile)
.to(outputFile)
.execute();
} finally {
// Stop the office process
OfficeUtils.stopQuietly(officeManager);
}
如果要转换其他格式, 只需要修改文件, 会根据文件后缀自动匹配:
File inputFile = new File("spreadsheet.xls");
File outputFile = new File("spreadsheet.ods");
JodConverter
.convert(inputFile)
.to(outputFile)
.execute();
当使用stream流时,可以显示指定其格式:
InputStream inputStream = ...
OutputStream outputStream = ...
JodConverter
.convert(inputStream)
.as(DefaultDocumentFormatRegistry.XLS)
.to(outputStream)
.as(DefaultDocumentFormatRegistry.ODS)
.execute();
问题: 每次使用都启动officeManager , 性能太低 .
在大多数应用中, 只需要启动一个OfficeManager , (如在servlet listener , spring context 配置时) , 当应用停止时,停止之. OfficeManager 自己会处理多线程.