简体   繁体   English

有没有办法只显示带有 webhdfs REST API 的文件?

[英]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 来访问HDFSHBase ,这阻止我使用配置文件和 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,我不能让这个弹出窗口出现。

  • Do any of you know if there is some data added to the document when using the OPEN operation ?有谁知道在使用OPEN操作时是否有一些数据添加到文档中?
  • If so is there any way to get rid of it ?如果是这样,有什么办法可以摆脱它吗?
  • Or is it something completly different that I'm missing ?还是我错过的完全不同的东西?

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 :如果您在这里遇到同样的问题,您可能需要验证:

  • Does this behavior appears on other browsers ?此行为是否出现在其他浏览器上? In my case no.在我的情况下没有。
  • Does this behavior appears with other files ?此行为是否出现在其他文件中? In my case no.在我的情况下没有。
  • Does this behavior appears when you try to open the file from your local system and not your browser ?当您尝试从本地系统而不是浏览器打开文件时,是否会出现此行为? In my case it did.在我的情况下确实如此。

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.

相关问题 使用 webHdfs 创建文件 - create file with webHdfs 使用REST API下载文件 - Download File with REST API 使用mmc rest API将日志文件从后端发送到UI-仅发送文件更改的逻辑 - send log file from backend to UI using mmc rest API- logic for sending only the changes in file REST API:使用GET请求匹配文件中的ID并显示与ID对应的对象 - REST API: Using a GET Request to match an ID within a File and display the object corresponding to ID Java - Retain only the Rest API base URL of a Rest API - Java - Retain only the Rest API base URL of a Rest API 有没有办法只显示过滤后的结果? - Is there a way to display only the filtered results? Java REST API 停止显示 Z93F725A07423FE1C889F448B33DZFZ 列表 - Java REST API stopped display java List 仅在使用APPLICATION_JSON的REST API上在TOMCAT上部署WAR文件才会引发错误 - Deploying WAR file on TOMCAT throws error only for REST API where APPLICATION_JSON used 有没有办法使用Marketo REST API从csv文件导入自定义对象数据? - Is there a way to import Custom Object data from csv file using the Marketo REST API? 在产生Excel或PDF文件作为响应的REST API中处理异常的正确方法是什么? - What is the correct way to handle exceptions in a REST API that produces an Excel or PDF file as response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM