我正在使用从RemoteConverter web应用程序到独立服务器的jBoss,该服务器构建为documents4j项目中包含的默认server-standalone。
在jboss内部,我已经得到了一个旧版本的必需库httpclient-4.0.1.jar和相关的httpcore-4.0.1.jar,因此我面临着由JVM加载的jar的不同版本引起的大量ClassDefNotFoundException。
HttpClientConnectionManager对象有一个特定的问题,尚未在版本中可用。
为了避免这个问题,我想为standalone-server定制一个http客户机,因为由于前面的问题,我不可能使用Jersey。
有人为这个standalone-server构建了不同的客户端吗?构建自定义RemoteClient的规范是什么?
更新1
在一个嗅探工具的帮助下进行了一些分析之后,我确定了消息的组成,因此我刚刚结束了该服务器的自定义HttpClient,如下所示:
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文件,那么它都是白色的,但是页数是正确的。如果使用文本编辑器打开文件并分析文件的开头和结尾,就会发现以下字符:
%PDF-1.5
%µµµµ
1 0 obj
[...]
startxref
1484122
%%EOF这似乎是一个良好的PDF文件。
还有其他与从REST服务器接收到的文件有关吗?
发布于 2016-05-05 22:07:06
在回答您的问题之前:我建议您选择shade,然后重新实现它。
我可以想象,在实现自己的服务时,您正面临编码问题。BufferedReader将输入的数据转换为字符数据。不要按字符行读取数据,而是将其视为二进制数据。而不是Writer类,而是使用Stream类。这就是为什么元数据被正确地处理为由字符表示,但实际数据是非法的,因为它是二进制信息:
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);
}https://stackoverflow.com/questions/37028962
复制相似问题