内容处置附件不起作用 - 将位打印到屏幕
我正在尝试在 Struts Action 类中下载 PDF 文件。 问题是,使用
response.setHeader("Content-Disposition", "attachment;filename=file.pdf");
我想打开“保存/打开”框,但现在 PDF 内容是在浏览器中写入的: 前任。
%PDF-1.4 28 0 obj << /Type /XObject /Subtype /Image /Filter /DCTDecode /Length 7746 /Width 200 /Height 123 /BitsPerComponent 8 /ColorSpace /DeviceRGB >>...(cut)
我在 Chrome、Firefox 和 IE 下尝试了这段代码(如下),在任何地方都是如此。我还为此使用了不同的 PDF 文件。
我的代码片段:
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;
还缺少什么吗?
编辑
当我在 Struts 类中使用此代码时,它不起作用,但当我在 Servlet 中使用此代码时,它可以工作。 最奇怪的是,当我在操作类中只向 Servlet 写入“response.sendRedirect()”(并且所有逻辑都在 Servlet 中)时,它也不起作用。
当我分析响应标头时,这三个示例中的所有内容都是相同的。
I'm trying to download a PDF file in Struts Action class.
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-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. Also I was using different PDF files for that.
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.
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.
When I analyzed the response headers everything in these three examples is the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 Content-Type 标头更改为浏览器无法识别的内容。而不是
使用
这将阻止浏览器对正文内容进行操作(包括通过插件对内容的处理),并将强制浏览器显示“保存文件”对话框。
此外,验证 Content-Disposition 标头中的分号后面是否存在单个空格对于触发所需的行为也可能很有用。因此,不要使用以下行,而是
使用以下行。
Try changing the Content-Type header to something not recognizable by the browser. Instead of
use
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. Therefore, instead of the following line
use the following instead.