【问题标题】:Webservice to convert XLS to PDF将 XLS 转换为 PDF 的 Web 服务
【发布时间】:2013-07-05 15:39:15
【问题描述】:

我正在开发一个生成 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 使用了正确的代码并搞砸了其他东西...... 任何建议表示赞赏!

【问题讨论】:

  • 您以text/plain 发送.xls 文件,您不认为它可能不正确吗?
  • 你说得对,我粘贴了这段代码并忽略了这个细节......无论如何,我将其更改为“multipart/form-data”并且异常发生了变化。我将其粘贴到问题正文中。

标签: java tomcat pdf-generation jodconverter


【解决方案1】:

起初您发送.xls 文件和Mime-Type 作为text/plain 并且您收到异常说由于某些错误它无法解析您的文档(可能您的JODConverter 2 试图转换.xls 文件如.txt 文件)。

这是第一个例外。现在你得到了 multipart/form-data 不受支持的异常。这个Mime-Type 从不代表一个文档,如果您想将另一个文档中的一个文档更改为 .pdf,您必须提供有效的 Mime-Type 定义。

对于.xls 文件,这个有效的Mime-Typeapplication/vnd.ms-excel。您可以在这里找到其他类型:mime-types for xls

此更改应允许您将请求发送到 JODConverter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 2016-01-22
    相关资源
    最近更新 更多