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 ...
随机推荐
- Bootstrap datetimepicker “dp.change” 时间/日期 选择事件
$('#<!--{$inputId}-->').datetimepicker({ todayHighlight: true, format: "YYYY-MM-DD<!-- ...
- RDMA over TCP的协议栈工作过程浅析
http://blog.chinaunix.net/uid-140205-id-2849342.html
- Windows Phone本地数据库(SQLCE):9、Connection Strings(翻译) (转)
这是“windows phone mango本地数据库(sqlce)”系列短片文章的第八篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...
- 老美的zxing和日本的qrcode哪个好?
ZXing用Java实现的多种格式的1D/2D条码图像处理库,Zxing库的主要部分支持以下几个功能:核心代码的使用.适用于J2SE客户端的版本.适用于Android客户端的版本(即BarcodeSc ...
- 美国谍梦第三至五季/全集The Americans迅雷下载
本季看点:冷战间谍题材美剧,FX电视台的<美国谍梦>老派谍战剧第二季开场吸引了190万的观众.在18-49岁的目标观众群中,这部福克斯电视工作室出品的剧集有160万的收视,提高了81%.一 ...
- 用jpinyin实现汉字转拼音功能
一.简介 项目地址:https://github.com/stuxuhai/jpinyin JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPiny ...
- Easyui 页面设置加载完成之后,满屏
js文件: if(top.location!=self.location){ top.location.href=self.location; }
- HTTPS安全证书介绍
IIS配置web SSL 安全证书Https访问 From : http://cao416451347ming.blog.163.com/blog/static/1154556162010217441 ...
- B. Random Teams(Codeforces Round 273)
B. Random Teams time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- verilog语法实例学习(3)
Verilog 操作运算符 算术运算符 +,-,*,/,**(加/减/乘/除/幂运算),乘法运算的结果的位宽是乘数和被乘数位宽的和. 在进行整数的除法运算时,结果要略去小数部分,只取整数部分:而进行取 ...