Javascript servlet pdf在chrome中显示为文本
所以我有一个servlet,它的URL类似于blah.do?params=xyz 在servlet中,我的代码类似于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[
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");