[英]Is there a way to only display a file with webhdfs REST API?
Hello StackOverflow community.你好 StackOverflow 社区。 I have been running into a problem lately regarding webhdfs REST API .
我最近遇到了一个关于webhdfs REST API的问题。
I have a servlet inside an application calling Apache Knox to access HDFS and HBase which prevents me from using configuration files and Hadoop base classes to solve my problem.我在调用Apache Knox的应用程序中有一个 servlet 来访问HDFS和HBase ,这阻止我使用配置文件和 Hadoop 基类来解决我的问题。 I had this solution before installing Knox and it worked fine but I obviously cannot use this anymore.
我在安装 Knox 之前有这个解决方案,它运行良好,但我显然不能再使用它了。
When I use the API to read a file with the op=OPEN
operation it downloads the file instead of just displaying it.当我使用API通过
op=OPEN
操作读取文件时,它会下载文件而不是仅显示它。 Which seems normal when I directly request it through an url like webhdfs/v1/path/to/storage/myPDF.pdf?op=OPEN
but when I try to call this URL from my code :当我通过像
webhdfs/v1/path/to/storage/myPDF.pdf?op=OPEN
这样的 url 直接请求它时,这似乎很正常,但是当我尝试从我的代码中调用这个 URL 时:
public byte[] requestDocumentKnox(String documentID, String documentType) {
byte[] response = null;
String surl = TSGVConstantes.KNOX_HDFS + SLASH + "v1" + SLASH + TSGVConstantes.HDFS_PATH + SLASH + documentID + "." + documentType + "?op=OPEN";
HttpURLConnection connection = null;
URL url;
try {
url = new URL(surl);
connection = (HttpURLConnection) url.openConnection();
String encoded = Base64.getEncoder().encodeToString((TSGVConstantes.HADOOP_USER + ":" + TSGVConstantes.KNOX_PASS).getBytes(StandardCharsets.UTF_8));
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + encoded);
if (connection.getResponseCode() != HTTP_CODE_OK) {
Trace.error(connection.getResponseCode() + " " + connection.getResponseMessage());
}
response = IOUtils.toByteArray(connection.getInputStream());
} catch (IOException ex) {
Trace.error(ex);
} finally {
if (connection != null) {
connection.disconnect();
}
}
return response;
}
private void returnDocument(HttpServletResponse response, byte[] document, String documentType) throws IOException {
ServletOutputStream output = null;
try {
output = response.getOutputStream();
if(document != null) {
response.setContentType(TSGVConstantes.getMimeTypeMap().get(documentType));
output.write(document);
} else {
// You don't need that
}
} catch (IOException ex) {
Trace.error(ex);
} finally {
response.flushBuffer();
if (output != null) {
output.close();
}
}
}
Both methods work correctly, they are called by some other methods, the document parameter from returnDocument()
is the byte[]
returned by requestDocumentKnox()
and there is no modification to that array between the calls.这两种方法都可以正常工作,它们被其他一些方法调用,
returnDocument()
的文档参数是requestDocumentKnox()
返回的byte[]
,并且在requestDocumentKnox()
调用之间没有对该数组进行修改。
This does display the PDF but also opens the print window of my web browser and tells me that it is possible that my pdf will not be displayed correctly.这确实显示了 PDF,但也会打开我的网络浏览器的打印窗口,并告诉我我的 pdf 可能无法正确显示。
My problem is : I need to get rid of that print pop up (and the warning) because my application is called inside others in order to display pdfs from HBase or HDFS, and I cannot let this pop up appears.我的问题是:我需要摆脱那个打印弹出窗口(和警告),因为我的应用程序在其他应用程序中被调用以显示来自 HBase 或 HDFS 的 pdf,我不能让这个弹出窗口出现。
OPEN
operation ?OPEN
操作时是否有一些数据添加到文档中? Thank you.谢谢你。
UPDATE更新
So apparently my problem does not appear on Chrome nor Edge but ONLY on Firefox (60.6.2esr (32 bits)).所以显然我的问题没有出现在 Chrome 和 Edge 上,而只出现在 Firefox(60.6.2esr(32 位))上。 I feel a bit stupid not having tried that on other browsers before.
之前没有在其他浏览器上尝试过,我觉得有点愚蠢。 Still I don't understand why and I cannot find a solution.
我仍然不明白为什么,我找不到解决方案。
ANOTHER UPDATE另一个更新
I found my problem, check my answer.我发现了我的问题,检查我的答案。
After some digging I finally found out.经过一番挖掘,我终于发现了。 If you do have the same problem here the things you might want to verify :
如果您在这里遇到同样的问题,您可能需要验证:
Conclusion : The problem was coming from the PDF and the viewer.结论:问题来自 PDF 和查看器。 I did not have the problem with other pdfs and other viewers.
我对其他 pdf 和其他查看器没有问题。 It seems like it was coming from Adobe Acrobat Document because when I tried to open it locally, the print pop up showed up as well.
它似乎来自Adobe Acrobat Document,因为当我尝试在本地打开它时,打印弹出窗口也出现了。
Hope this helps.希望这可以帮助。
PS : You might also want to check the settings of your brower regarding how it handles pdf. PS:您可能还想检查浏览器的设置,了解它如何处理 pdf。 Here is a link that can help for firefox .
这是一个可以帮助firefox的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.