简体   繁体   English

内容处置附件不起作用 - 将位打印到屏幕

[英]Content-Disposition attachment doesn't work - prints bits to screen

I'm trying to download a PDF file in Struts Action class.我正在尝试在 Struts Action class 中下载 PDF 文件。 Problem is that using问题是使用

response.setHeader("Content-Disposition", "attachment;filename=file.pdf");

I want to open box for "save/open", but now PDF content is writen in browser: ex.我想打开“保存/打开”框,但现在 PDF 内容写在浏览器中:例如。

%PDF-1.4 28 0 obj << /Type /XObject /Subtype /Image /Filter /DCTDecode /Length 7746 /Width 200 /Height 123 /BitsPerComponent 8 /ColorSpace /DeviceRGB >>...(cut)

I was trying this code (below) under Chrome, Firefox and IE and everywhere this same.我在 Chrome、Firefox 和 IE 下尝试了这段代码(如下),在任何地方都一样。 Also I was using different PDF files for that.我也为此使用了不同的 PDF 文件。

My code fragment:我的代码片段:

try {
    URL fileUrl = new URL("file:///" + filePath);
    URLConnection connection = fileUrl.openConnection();
    inputStream = connection.getInputStream();
    int fileLength = connection.getContentLength();
    byte[] outputStreamBytes = new byte[100000];
    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment;filename=file.pdf");
    response.setContentLength(fileLength);
    outputStream = response.getOutputStream();
    int iR;
    while ((iR = inputStream.read(outputStreamBytes)) > 0) {
        outputStream.write(outputStreamBytes, 0, iR);
    }

    return null;
} catch (MalformedURLException e) {
    logger.debug("service", "An error occured while creating URL object for url: "
        + filePath);
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return null;
} catch (IOException e) {
    logger.debug("service", "An error occured while opening connection for url: "
        + filePath);
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return null;
} finally {
    if (outputStream != null) {
        outputStream.close();
    }
    if (inputStream != null) {
        inputStream.close();
    }
    inputStream.close();
}
return null;

Is something still missing?还缺少什么吗?

EDIT编辑

When I use this code in the Struts class it does not work, but when I use this code in the Servlet it is working.当我在 Struts class 中使用此代码时,它不起作用,但是当我在 Servlet 中使用此代码时,它正在工作。 The strangest thing is that when I in action class write only "response.sendRedirect()" to Servlet (and all logic is in Servlet) it is not working too.最奇怪的是,当我在操作 class 时只向 Servlet 写入“response.sendRedirect()”(所有逻辑都在 Servlet 中),它也不起作用。

When I analyzed the response headers everything in these three examples is the same.当我分析响应标头时,这三个示例中的所有内容都是相同的。

Try changing the Content-Type header to something not recognizable by the browser.尝试将 Content-Type header 更改为浏览器无法识别的内容。 Instead of代替

response.setContentType("application/pdf");

use利用

response.setContentType("application/x-download");

This will prevent the browser from acting upon the content of the body (which includes the handling of the content by plugins), and will force the browser to display the 'Save File' dialog box.这将阻止浏览器对正文的内容进行操作(包括插件对内容的处理),并将强制浏览器显示“保存文件”对话框。

Additionally, it might also be useful to verify if the presence of a single whitespace after the semicolon in the Content-Disposition header, is necessary to trigger the required behavior.此外,验证 Content-Disposition header 中分号后是否存在单个空格对于触发所需行为是否是必要的也可能很有用。 Therefore, instead of the following line因此,而不是以下行

response.setHeader("Content-Disposition", "attachment;filename=file.pdf");

use the following instead.请改用以下内容。

response.setHeader("Content-Disposition", "attachment; filename=file.pdf");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Chrome 不遵守 Content-Disposition Attachment 指令 - Chrome not honoring Content-Disposition Attachment directive 在RemoteServiceServlet中使用内容处置附件 - Use content-disposition attachment in RemoteServiceServlet 如何设置“附件”预选的“打开”内容处置 - How to set “Open” preselected with “attachment” Content-disposition 第一行附件前的 JavaMail Content-Disposition - JavaMail Content-Disposition before first line of attachment 内联内容处置 - Content-disposition inline 浏览器和邮递员不会在 Content-Disposition 标头中解码 UTF-8 文件名 - Browsers and Postman doesn't decode UTF-8 filename in Content-Disposition header 在一起使用jfree和itext时,无法将Response的内容处置设置为附件 - Response's content-disposition cannot be set to attachment while using jfree and itext together 如果附件文件大小超过 16363 字节,Content-Disposition 不会在 Spring 引导应用程序中响应 - If attachment file size is more than 16363byte Content-Disposition is not comming in response in Spring boot application 用Java读取内容处置文件 - Read file of content-disposition with java AngularJS 标题中的“Content-Disposition”和文件名 - "Content-Disposition" and file name in headers with AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM