Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文档导出为PDF会产生Docx4JException_Java_Http_Docx4j - Fatal编程技术网

Java 将文档导出为PDF会产生Docx4JException

Java 将文档导出为PDF会产生Docx4JException,java,http,docx4j,Java,Http,Docx4j,我正在尝试将一个文档导出为PDF,该文档并不重要,因为我在一个空白文档中遇到了相同的错误。 我对gradle使用docx4j是因为我没有maven方面的经验,我认为这不应该是个问题 从build.gradle剪下 // docx4j implementation("org.docx4j:docx4j-JAXB-ReferenceImpl:11.2.8") 这是我的简单类OpenDoc import java.io.File; import java.io.IOExcepti

我正在尝试将一个文档导出为PDF,该文档并不重要,因为我在一个空白文档中遇到了相同的错误。 我对gradle使用docx4j是因为我没有maven方面的经验,我认为这不应该是个问题

从build.gradle剪下

// docx4j
implementation("org.docx4j:docx4j-JAXB-ReferenceImpl:11.2.8")
这是我的简单类OpenDoc

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import org.docx4j.Docx4J;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

public class OpenDoc
{
  public static void main(final String[] args)
  {
    final String filePath = "AnyFile.docx";
    try
    {
      final WordprocessingMLPackage word = Docx4J.load(new File(filePath));
      final MainDocumentPart doc = word.getMainDocumentPart();

      final OutputStream outputSteam = new OutputStream()
      {
        @Override
        public void write(final int arg0) throws IOException
        {
          // TODO kase: Auto-generated method stub
        }
      };
      Docx4J.toPDF(word, outputSteam);
    }
    catch (final Docx4JException e)
    {
      e.printStackTrace();
    }
  }
}
所以问题是我运行程序时出现的错误

    10:45:47.557 [main] DEBUG org.docx4j.openpackaging.io3.Save - ...Done!
    10:45:47.561 [main] DEBUG org.docx4j.services.client.ConverterHttp - starting, with endpointURL: http://localhost:9016/v1/00000000-0000-0000-0000-000000000000/convert
    10:45:47.807 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
    10:45:47.815 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
    10:45:47.816 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://localhost:9016][total available: 0; route allocated: 0 of 2; total allocated: 0 of 20]
    10:45:47.832 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://localhost:9016][total available: 0; route allocated: 1 of 2; total allocated: 1 of 20]
    10:45:47.833 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {}->http://localhost:9016
    10:45:47.842 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to localhost/127.0.0.1:9016
    10:45:49.891 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connect to localhost/127.0.0.1:9016 timed out. Connection will be retried using another IP address
    10:45:49.891 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to localhost/0:0:0:0:0:0:0:1:9016
    10:45:51.936 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Shutdown connection
    10:45:51.936 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
    10:45:51.936 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://localhost:9016][total available: 0; route allocated: 0 of 2; total allocated: 0 of 20]
    10:45:51.937 [main] ERROR org.docx4j.services.client.ConverterHttp - 
    Looks like your endpoint URL 'http://localhost:9016/v1/00000000-0000-0000-0000-000000000000/convert' is wrong
    
    10:45:51.937 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection manager is shutting down
    10:45:51.937 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection manager shut down
    org.docx4j.openpackaging.exceptions.Docx4JException: This behaviour may be Windows client OS specific; please look in the server logs or try a Linux client
        at org.docx4j.Docx4J.toPDF(Docx4J.java:764)
        at docx4j.OpenDoc.main(OpenDoc.java:34)
    Caused by: org.docx4j.services.client.ConversionException: This behaviour may be Windows client OS specific; please look in the server logs or try a Linux client
        at org.docx4j.services.client.ConverterHttp.execute(ConverterHttp.java:248)
        at org.docx4j.services.client.ConverterHttp.convert(ConverterHttp.java:190)
        at org.docx4j.Docx4J.toPDF(Docx4J.java:761)
        ... 1 more
    Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:9016 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
...

我为自己找到了解决办法,也许将来其他人也会遇到同样的问题:D

我刚刚将docx4j export fo添加到gradle构建文件中,它可以工作,因为PDF转换器只被移动到子项目中

implementation 'org.docx4j:docx4j-export-fo:11.2.8'