1. 程式人生 > >移動端網頁開啟檢視 pdf 檔案

移動端網頁開啟檢視 pdf 檔案

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了