前端部分
//得到拼接字符串 function getFJInfo(name, url) { return "<tr><td style=''><a href='javascript:void(0)' οnclick='getDownFile(\"" + url + "\",\"" + name + "\")'>" + name + "</a></td></tr>" } //文件下載 function getDownFile(url, name) { var param = { "url": url }; $.ajax({ url: contextPath + '/product-label/file2Stream', type: 'GET', data: Base64.encode(JSON.encode(param)), dataType: "text", success: function(data) { downloadFile(name, data) } }) } //流處理觸發下載事件 function downloadFile(fileName, content) { var aLink = document.createElement('a'); var blob = new Blob([content]); var evt = document.createEvent("MouseEvents"); evt.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); aLink.download = fileName; aLink.href = URL.createObjectURL(blob); aLink.dispatchEvent(evt) }
后台controller處理,參考http://blog.sina.com.cn/s/blog_87216a0001014sm7.html
/** * 返回流 * * @param requestMap 請求參數 * @param response 返回對象 */ @RequestMapping(value = "/file2Stream", method = RequestMethod.GET) public void file2Stream(@Json Map<String, Object> requestMap, HttpServletResponse response) { try { String url = String.valueOf(requestMap.get("url")); // URL url =new URL(String.valueOf(requestMap.get("url"))); InputStream iStream = getFileStream(url); OutputStream stream = response.getOutputStream(); stream.write(StreamUtils.getBytes(iStream)); stream.flush(); stream.close(); } catch (Exception e) { LOG.error("ProductSalesRecommendController.file2Stream error | ({})", e); } } /** * HttpURLConnection獲取網絡路徑的文件流 * * @param url 鏈接 * @return InputStream * @throws IOException */ private InputStream getFileStream(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("GET"); InputStream inStream = conn.getInputStream(); return inStream; } /** * HttpClient獲取網絡路徑的文件流 * * @param url 鏈接字符串 * @return InputStream * @throws IllegalStateException * @throws IOException */ private InputStream getFileStream(String url) throws IllegalStateException, IOException { HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 5000); // 設置連接超時為5秒 HttpClient client = new DefaultHttpClient(httpParams); // 生成一個http客戶端發送請求對象 HttpResponse httpResponse = client.execute(new HttpGet(url)); // 發送請求並等待響應 HttpEntity entity = httpResponse.getEntity(); // 獲取響應里面的內容 InputStream inStream = entity.getContent(); return inStream; }
//得到拼接字符串function getFJInfo(name, url) {return "<tr><td style=''><a href='javascript:void(0)' οnclick='getDownFile(\"" + url + "\",\"" + name + "\")'>" + name + "</a></td></tr>"}//文件下載function getDownFile(url, name) {var param = {"url": url};$.ajax({url: contextPath + '/product-label/file2Stream',type: 'GET',data: Base64.encode(JSON.encode(param)),dataType: "text",success: function(data) {downloadFile(name, data)}})}//流處理觸發下載事件function downloadFile(fileName, content) {var aLink = document.createElement('a');var blob = new Blob([content]);var evt = document.createEvent("MouseEvents");evt.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);aLink.download = fileName;aLink.href = URL.createObjectURL(blob);aLink.dispatchEvent(evt)}