用于Documents4j的RemoteConverter的自定义HttpClient [英] Custom HttpClient for RemoteConverter of Documents4j

查看:751
本文介绍了用于Documents4j的RemoteConverter的自定义HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jBoss Web应用程序中使用 RemoteConverter 到作为默认 server-standalone构建的独立服务器包含在documents4j项目中。

I am using a RemoteConverter from a jBoss web application to a standalone server built as the default server-standalone included into documents4j project.

在jboss内部我有一个旧版本的必需库 httpclient-4.0.1.jar 和相关的 httpcore-4.0.1.jar 所以我面临着大量的 ClassDefNotFoundException JVM加载的jar的不同版本。

Inside jboss I've got an old version of required libraries httpclient-4.0.1.jar and related httpcore-4.0.1.jar so I'm facing with a lot of ClassDefNotFoundException caused by the different version of the jar loaded by JVM.

HttpClientConnectionManager 对象存在特定问题在版本中

There is a specific problem with HttpClientConnectionManager object that is not available yet in version

为了避免这个问题,我想为独立服务器加入自定义http客户端,因为以前的问题,我不可能使用 Jersey

To avoid this problem I'd like to bluild a custom http client for the standalone-server, because, due to the previous problems, it's not possible for me to use Jersey.

是否有人建立了不同的那个独立服务器的客户端?有哪些规格可以构建自定义 RemoteClient

Has someone build a different client for that standalone-server? What are the specs to build a custom RemoteClient?

更新1

在借助嗅探工具进行一些分析之后,我想出了消息的组成,所以我刚刚结束了自定义 HttpClient该服务器的如下:

After a little bit of analysis with the help of a sniffing tool, I figured out the composition of the message, so I've just ended a custom HttpClient for that server as following:

    File wordFile = new File("C:/temp/test.docx");
    InputStream targetStream = new FileInputStream(wordFile);

    URL url = new URL("http://localhost:9998");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/vnd.com.documents4j.any-msword");
    conn.setRequestProperty("Accept", "application/pdf");
    conn.setRequestProperty("Converter-Job-Priority", "1000");


    OutputStream os = conn.getOutputStream();
    os.write(IOUtils.toByteArray(targetStream));
    os.flush();

    if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new RuntimeException("Failed : HTTP error code : "
            + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));
    FileWriter ostream = new FileWriter("C:/temp/test.pdf");
    BufferedWriter out = new BufferedWriter(ostream);
    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
        out.write(output+"\n");
    }
    br.close();
    out.close();
    os.close();
    conn.disconnect();

现在我遇到了另一个问题,如果我尝试打开刚刚创建的test.pdf文件,那么全白,但页数正确。如果我用文本编辑器打开文件并分析文件的开头和结尾,我发现以下字符:

Now I've got another problem, if I try to open just created test.pdf file it is all white but with the right number of pages. If I open the file with a text editor and analyze the beginning and the end of file I found the following chars:

%PDF-1.5
%µµµµ
1 0 obj  
[...]
startxref
1484122
%%EOF

这似乎是一个很好的PDF文件。

It seems to be a good PDF file.

还有其他事要做从REST服务器收到该文件?

There is something else to do with that file received from REST Server?

推荐答案

在回答你的问题之前:我建议你宁愿 shade 依赖关系然后重新实现它。

Before answering your question: I would recommend you to rather shade the dependency then to reimplement it.

我可以想象您在实现自己的服务时遇到编码问题。 BufferedReader 将传入数据转换为字符数据。不要按字符行读取数据,而是将其视为二进制。而不是 Writer 类使用 Stream 类。这就是元数据被正确处理的原因,因为它由字符表示,但实际数据是非法的,因为它是二进制信息:

I can imagine that you are facing an encoding issue when implementing your own service. A BufferedReader translates the incoming data to character data. Do not read data by character line but treat it as binary. Instead of Writer classes use the Stream classes. This is why the meta data is treated correctly as it is represented by characters but the actual data is illegal as it is binary information:

InputStream in = conn.getInputStream();
OutputStream out = new FileOutputStream("C:/temp/test.pdf");
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
    out.write(buffer, 0, len);
}

这篇关于用于Documents4j的RemoteConverter的自定义HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆