版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1.首先下载 pdf.js
2.将pdf解压,放入项目中webapp下
3.后台处理:根据url获取pdf,把数据写入到输出流
js:
download: function (url) { var urls = '${contextPath}/***/open?urls='+url; window.location.href = "${ctxStatic}/pdf/web/viewer.html?file="+encodeURIComponent(urls); },
后台:
@RequestMapping(value = "/open",method = RequestMethod.GET) public void open(String urls,HttpServletResponse response) throws IOException { URL httpUrl=new URL(urls); HttpURLConnection httpURLConnection=(HttpURLConnection) httpUrl.openConnection(); InputStream inputStream=httpURLConnection.getInputStream(); response.setHeader("Content-disposition", "attachment; filename=" + new String( "PDF文件名".getBytes("gb2312"), "ISO8859-1" ) ); response.setContentType("multipart/form-data"); OutputStream outputStream = response.getOutputStream(); IOUtils.write(IOUtils.toByteArray(inputStream),outputStream); }4.调用事件,就会打开pdf了