文件下载 浏览器直接打开文件而不是保存 相关问题
2016-02-02 10:04
896 查看
昨天做SpringMVC 文件上传下载功能时遇到一堆问题。其中有个问题就是txt css js html xml pdf 等等文件下载时浏览器(html5的a标签download属性不是所有浏览器都支持的)是直接打开,而不是下载保存。网上有许多解决的方法,我这边只是整合下,做个mark。
场景就是有个url,老大提醒说用后台访问url获取文件流 前台来处理。。大概这个流程 代码如下:
前端js部分,参考http://www.alloyteam.com/2014/01/use-js-file-download/
后台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;
}
先这样吧。。老大说还有更高大上的。过段时间再看看
场景就是有个url,老大提醒说用后台访问url获取文件流 前台来处理。。大概这个流程 代码如下:
前端js部分,参考http://www.alloyteam.com/2014/01/use-js-file-download/
//得到拼接字符串 function getFJInfo(name, url) { return "<tr><td style=''><a href='javascript:void(0)' onclick='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;
}
先这样吧。。老大说还有更高大上的。过段时间再看看
相关文章推荐
- OBlog任意文件下载漏洞
- php隐藏实际地址的文件下载方法
- java实现适用于安卓的文件下载线程类
- php利用header函数实现文件下载时直接提示保存
- asp.net 文件下载的通用方法
- JSP文件下载功能的4种方法
- JSP隐含对象response实现文件下载
- JSP response对象实现文件下载的两种方式
- php实现文件下载功能的几个代码分享
- php实现文件下载实例分享
- PHP实现文件下载详解
- PHP实现文件下载断点续传详解
- PHP+Mysql+jQuery文件下载次数统计实例讲解
- php实现文件下载代码分享
- IIS支持exe文件下载配置方法附图
- VBS下载者(通过vbscript实现文件下载)
- php限制文件下载速度的代码
- JavaEE实现文件下载
- Android通过startService实现文件批量下载
- PHP 下载文件时自动添加bom头的方法实例