通过a连接下载服务器上的pdf文件

来源:互联网 发布:ppt模板软件 编辑:程序博客网 时间:2024/06/05 12:31

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">服务器上面的pdf文件,直接href="http://路径"会默认为打开;</span>

解决方案:

1.通过a指向一个Action请求

aPdf.href="process/downloadPdf?pdfURL="+pdfURL;

pdf为服务器文件路径

2.java代码

public void downloadPdf() {HttpServletResponse response = getResponse();InputStream is = null;OutputStream ous = null;try {URL url = new URL(pdfURL);URLConnection connection = url.openConnection();is = connection.getInputStream();byte[] buffer = new byte[is.available()];is.read(buffer);is.close();response.reset();String fileName = pdfURL.substring(pdfURL.lastIndexOf("/")+1);response.addHeader("Content-Disposition","attachment;filename='"+fileName+"'");ous = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");ous.write(buffer);ous.flush();ous.close();} catch (IOException e) {e.printStackTrace();} finally {try {if (null != is) {is.close();}if (null != ous) {ous.close();}} catch (IOException e) {throw new RuntimeException("关闭失败");}}}


0 0
原创粉丝点击