文件下载 浏览器直接打开文件(前端控制)
前端部分
//得到拼接字符串 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)}