Springmvc 服务器端文件下载
转自:http://blog.csdn.net/boneix/article/details/51303280
业务场景:点击下载后直接保存而不是打开
解决代码:前端传入url
/**
* 返回流
*
* @param requestMap 请求参数
* @param response 返回对象
*/
@RequestMapping(value = "/file2Stream", method = RequestMethod.GET)
public void file2Stream(@Json Map<String, Object> requestMap, HttpServletResponse response) {
InputStream iStream = null;
OutputStream outStrem = null;
try {
String url = String.valueOf(requestMap.get("url"));
iStream = getFileStream(url);
String fileName = String.valueOf(requestMap.get("fileName"));
fileName = new String(fileName.getBytes(), "ISO8859-1");
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
outStrem = response.getOutputStream();
outStrem.write(StreamUtils.getBytes(iStream));
outStrem.flush();
} catch (Exception e) {
LOG.error("ProductSalesRecommendController.file2Stream error | ({})", e);
}finally {
if(iStream != null){
try {
iStream.close();
iStream = null;
} catch (IOException e) {
LOG.error("file2Stream.InputStream.close error | ({})", e);
}
}
if(outStrem != null){
try {
outStrem.close();
outStrem = null;
} catch (IOException e) {
LOG.error("file2Stream.OutputStream.close error | ({})", e);
}
}
}
}
/**
* HttpClient获取网络路径的文件流
*
* @param url 链接字符串
* @return InputStream
* @throws IllegalStateException
* @throws IOException
*/
private InputStream getFileStream(String url)
throws IllegalStateException, IOException {
InputStream inStream = new URL(url).openStream();
return inStream;
}
Springmvc 服务器端文件下载的更多相关文章
- 基于 Nginx XSendfile + SpringMVC 进行文件下载
转自:http://denger.iteye.com/blog/1014066 基于 Nginx XSendfile + SpringMVC 进行文件下载 PS:经过实际测试,通过 nginx 提供文 ...
- SpringMVC实现文件下载的两种方式及多文件下载
1.传统方法 @RequestMapping("/download") public String download( String fileName ,String filePa ...
- springmvc实现文件下载
springmvc实现文件下载 使用springmvc实现文件下载有两种方式,都需要设置response的Content-Disposition为attachment;filename=test2.p ...
- 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题
问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...
- SpringMVC 服务器端验证
1.导入JSR303验证类库Jar包2.在MVC的配置文件中添加<mvc:annotation-driven/>的配置3.在MVC的配置文件中添加验证器的配置4.在接收表单数据的类中添加验 ...
- SpringMVC,SpringBoot文件下载
前言 最近严查security, 导致原来暴露出去的s3不能用了,不允许public的s3,暂时的折中方案是自己做跳转.于是需要在SpringMVC中实现文件下载功能. 关于文件存储的设计 文件存储通 ...
- springmvc实现文件下载到Android手机设备pda端
1:首先要添加相关得jar文件,在pom.xml中 <dependency> <groupId>commons-fileupload</groupId> <a ...
- .net 服务器端文件下载
string path = Server.MapPath("/Source/mjpjb.rar"); System.IO.FileInfo file = new System.IO ...
- SpringMVC实现文件下载时,请求路径中的扩展名被省略
问题描述 问题是这样的,我写了一个DownloadController,用来处理下载请求,预期效果如下: 客户端浏览器在访问URL --> http://localhost:8080/ssm ...
随机推荐
- 在ASP.NET MVC下扩展一个带验证的RadioButtonList
在ASP.NET MVC4中,HtmlHelper为我们提供了Html.RadioButton()方法用来显示Radio Button单选按钮.如果想显示一组单选按钮,通常的做法是遍历一个集合把每个单 ...
- SpringBoot 中使用 @Value 为 static 变量赋值
原文:https://www.jianshu.com/p/ea477fc9abf7 例如: public class Utils { @Value("${test.host}") ...
- C#编程(五十八)----------并行集合
并行集合 对于并行任务,与其相关紧密的就是对一些共享资源,数据结构的并行访问.经常要做的就是对一些队列进行加锁-解锁,然后执行类似插入,删除等等互斥操作. .NET4提供了一些封装好的支持并行操作数据 ...
- C#编程(三十六)----------元组
元组 数组合并了相同类型的对象,而元组合并了不同类型的对象. .NET 4定义了8个泛型Tuple类和一个静态的Tuple类,他们用作元组的工厂. 元组是一种数据结构,通过逗号分隔 Tuple< ...
- <fmt:formatNumber>标签
<fmt:formatNumber>标签用于格式化数字,百分比,货币. 属性 <fmt:formatNumber>标签有如下属性: 属性 描述 是否必要 默认值 value 要 ...
- 【Elasticsearch】ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC 2017] with format [yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis]];
ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC ...
- Python中的文件IO操作(读写文件、追加文件)
Python中文件的读写包含三个步骤:打开文件,读/写文件,关闭文件. 文件打开之后必须关闭,因为在磁盘上读写文件的功能是由操作系统提供的,文件作为对象,被打开后会占用操作系统的资源,而操作系统在同一 ...
- 亿万第一至二季/全集Billions迅雷下载
英文全名Billions,第1季(2016)Showtime. 本季看点:<亿万>讲述了纽约市政治与经济领域.关于金钱的一场较量.故事主要描述两个华尔街重量级人物之间的战争.精明.强硬的检 ...
- JAVA中对List<map<String,Object>>根据map某个key值进行排序
方法compareTo()比较此对象与指定对象的顺序.如果该对象小于.等于或大于指定对象,则分别返回负整数.零或正整数.返回整数,1,-1,0:返回1表示大于,返回-1表示小于,返回0表示相等. 普通 ...
- [转]Hive安装及使用攻略
转 : http://blog.fens.me/hadoop-hive-intro/