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 ...
随机推荐
- 【防火墙技术连载11】强叔侃墙 攻击防范篇 流量型攻击之UDP Flood及防御
http://support.huawei.com/huaweiconnect/enterprise/thread-214141.html
- Linux Delay Accounting
https://andrestc.com/post/linux-delay-accounting/ Ever wondered how long is your program spending wh ...
- Revit API取得全部元素
; ; ; ; } ; } ; } TaskDialog ...
- C#编程(五十九)----------集合的性能
各种集合的性能 许多集合类提供了相同的功能,例如,SortedList类与SortedDictionary类的功能几乎完全相同.但是,其性能常常有很大的区别.SortedList集合使用的内存少,So ...
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展(实例)
(1)clean var arr = [1,2,null,3,'']; alert(Ext.Array.clean(arr)); //clean的对象:(value === null) || (val ...
- Swift - EasingAnimation绘制圆环动画
Swift - EasingAnimation绘制圆环动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CircleView ...
- linux中解压rar文件
linux平台默认是不支持RAR文件的解压,需要安装linux版本的RAR压缩软件,下载地址为:http://www.rarlab.com/download.htm 下载之后进行解压之后,进入rar目 ...
- 修复android下webView控件的总结
游戏中有一个收集玩家问题反馈的网页,很早之前就有同事反映说android在游戏无法上传附件,在浏览器中是可以正常使用的.最近能腾出手来的时候,就仔细看了一下这个问题,发现很里藏着不少问题,这里一一记录 ...
- Xcode打包踩过的那些坑
一.file was built for archive which is not the architecture being linked (armv7s) 项目是基于cocos2d-x绑定lua ...
- 让Java和JavaScript进行交互
本篇博文参考自:http://droidyue.com/blog/2014/09/20/interaction-between-java-and-javascript-in-android/ 我们在使 ...