我正在开发一个生成XLS文件作为输出的Java小程序。在我让用户查看,下载和打印文档之前,我需要将其转换为PDF(甚至更好的PDF / A),因为我需要它不可修改。
我尝试编写解决方案,但它确实有效。我在我的applet中下载并捆绑了JODConverter 2,以便输出XLS成为JODConverter输入文件,一切正常。问题是这个组件的大小:差不多2mb。由于我的applet已经是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();
}
}
}
我下载了一个WAR,其中包括开箱即用的JODConverter Web服务并将其放入我的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代码,搞砸了别的东西...... 任何建议表示赞赏!
答案 0 :(得分:1)
首先,您发送的.xls
文件Mime-Type
为text/plain
并且您遇到异常,表示由于某些错误而无法解析您的文档(可能是您的JODConverter 2正在尝试将.xls
文件转换为.txt
文件)。
这是第一个例外。现在您将获得multipart/form-data
未被移植的异常。此Mime-Type
永远不代表文档,如果您想将另一个文档更改为.pdf,则必须提供有效的Mime-Type
定义。
对于.xls
个文件,此有效Mime-Type
为application/vnd.ms-excel
。您可以在此处找到其他类型:mime-types for xls。
此更改应允许您将请求发送到JODConverter