Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript servlet pdf在chrome中显示为文本_Javascript_Pdf_Servlets - Fatal编程技术网

Javascript servlet pdf在chrome中显示为文本

Javascript servlet pdf在chrome中显示为文本,javascript,pdf,servlets,Javascript,Pdf,Servlets,所以我有一个servlet,它的URL类似于blah.do?params=xyz 在servlet中,我的代码类似于 ServletOutputStream out = response.getOutputStream(); request.setAttribute("Content-Type","application/pdf"); request.setAttribute("Content-Disposition","attachment;filename=test.pdf"); byte[

所以我有一个servlet,它的URL类似于blah.do?params=xyz

在servlet中,我的代码类似于

ServletOutputStream out = response.getOutputStream();
request.setAttribute("Content-Type","application/pdf");
request.setAttribute("Content-Disposition","attachment;filename=test.pdf");
byte[] bytes = SystemServer.getFileContents(fileId).getBytes();      
request.setAttribute("Content-Length","" + bytes.length);
out.write(bytes, 0, bytes.length);
out.flush();
我用

window.open(url,"my file","someparams");
但chrome正在以纯文本的形式打开窗口,并且view source确认输出的所有内容都是

%PDF-1.4 %áéëÓ 2 0 obj  ..... all contents....%%EOF
那么我怎样才能强迫它以pdf格式出现呢


我使用相同的代码将图像返回到浏览器,效果很好

您需要在响应对象而不是请求上设置这些属性。

您正在将一些内容设置为请求属性,这些属性实际上应该在响应上设置

response.setContentType("application/pdf");
response.setContentLength(bytes.length);
response.addHeader("Content-Disposition","attachment;filename=test.pdf");