我正在开发一个生成 XLS 文件作为输出的 Java 小程序。在让用户查看、下载和打印文档之前,我需要将其转换为 PDF(甚至更好的 PDF/A),因为我需要它不可修改。
我尝试编写一个解决方案,它可以工作。我在我的小程序中下载并捆绑了 JODConverter 2,以便输出 XLS 成为 JODConverter 输入文件,一切正常。问题是这个组件的大小:几乎 2mb。由于我的小程序已经是 3mb,我也不想将 JODConverter 捆绑在其中......
我在文档中读到它也可以作为 Web 服务工作:我创建一个 POST 请求,将其发送到服务并获取文件,所有这些都无需下载一个 kb 的 JODConverter。听起来不错,但我无法让它工作。
下面是我写的代码:
public class Main
{
public static void main(String[] args) throws Exception
{
doPost(new URL("http://localhost:8090/pdfconverter/service"), "C:\\Documents and Settings\\Administrator\\Desktop\\Gestione oneri\\calcolo oneri XP.xls");
}
private static void doPost(URL url, String binaryFile)
{
try
{
File binFile = new File(binaryFile);
URLConnection conn = url.openConnection();
conn.addRequestProperty("Content-Type", "multipart/form-datastrong text");
conn.addRequestProperty("Accept", "application/pdf");
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
FileInputStream input = new FileInputStream(binFile);
byte[] buffer = new byte[1024];
for(int length = 0; (length = input.read(buffer)) > 0;)
{
wr.write(buffer, 0, length);
}
wr.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
// Get the response and write it to a file.
File file = new File("C:\\Documents and Settings\\Administrator\\Desktop\\Gestione oneri\\calcolo oneri XP2.pdf");
FileOutputStream wrFile = new FileOutputStream(file);
DataInputStream dataInput = new DataInputStream(conn.getInputStream());
buffer = new byte[1024];
for(int length = 0; (length = dataInput.read(buffer)) > 0;)
{
wrFile.write(buffer, 0, length);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我下载了一个开箱即用的包含 JODConverter Web 服务的 WAR,并将其放入我的 tomcat\webapps\pdfconverter,创建了启动 OpenOffice 服务的脚本
soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard
但这是我的堆栈跟踪:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8090/pdfconverter/service
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at main.Main.doPost(Main.java:128)
at main.Main.main(Main.java:25)
其中 Main.java:128 是该行
DataInputStream dataInput = new DataInputStream(conn.getInputStream());
Tomcat 日志显示: 访问日志:
127.0.0.1 - - [30/Jan/2013:11:45:24 +0100] "POST /pdfconverter/service HTTP/1.1" 500 4426
[将内容类型从 text/plain 更改为 multipart/form-data 后编辑] 标准日志:
Grave: Servlet.service() for servlet [DocumentConverterServiceServlet] in context with path [/pdfconverter] threw exception
java.lang.IllegalArgumentException: unsupported input mime-type: multipart/form-data
at com.artofsolving.jodconverter.web.DocumentConverterServiceServlet.doPost(DocumentConverterServiceServlet.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
我认为POST有问题,因为在转换过程中发生了异常,但我似乎找不到问题:发布的代码是各种试验和错误的最后一个版本,所以可能在此之前我使用正确的 POST 代码并搞砸了其他东西......任何建议都值得赞赏!