我是javascript新手,我有一个要求,我需要在服务器上单击“提交”按钮时打开pdf文件。我还需要将其重定向到新的jsp。我尝试使用window.open,但由于无法从服务器位置获取文件而无法打开。href也不起作用。有人可以建议我吗?
可以通过不同的方式进行操作,如果您正在使用servlet,则可以将正确的MIME类型写入响应中,并将PDF写入servlet流中
就像是:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf;charset=UTF-8");
response.addHeader("Content-Disposition", "inline; filename=" + "cities.pdf");
ServletOutputStream out = response.getOutputStream();
InputStream inputStream = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
inputStream = new FileInputStream("sourcePath_of_pdf.pdf");
byte[] buffer = new byte[1024];
baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
baos.writeTo(out);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句