转自: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 服务器端文件下载的更多相关文章

  1. 基于 Nginx XSendfile + SpringMVC 进行文件下载

    转自:http://denger.iteye.com/blog/1014066 基于 Nginx XSendfile + SpringMVC 进行文件下载 PS:经过实际测试,通过 nginx 提供文 ...

  2. SpringMVC实现文件下载的两种方式及多文件下载

    1.传统方法 @RequestMapping("/download") public String download( String fileName ,String filePa ...

  3. springmvc实现文件下载

    springmvc实现文件下载 使用springmvc实现文件下载有两种方式,都需要设置response的Content-Disposition为attachment;filename=test2.p ...

  4. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

  5. SpringMVC 服务器端验证

    1.导入JSR303验证类库Jar包2.在MVC的配置文件中添加<mvc:annotation-driven/>的配置3.在MVC的配置文件中添加验证器的配置4.在接收表单数据的类中添加验 ...

  6. SpringMVC,SpringBoot文件下载

    前言 最近严查security, 导致原来暴露出去的s3不能用了,不允许public的s3,暂时的折中方案是自己做跳转.于是需要在SpringMVC中实现文件下载功能. 关于文件存储的设计 文件存储通 ...

  7. springmvc实现文件下载到Android手机设备pda端

    1:首先要添加相关得jar文件,在pom.xml中 <dependency> <groupId>commons-fileupload</groupId> <a ...

  8. .net 服务器端文件下载

    string path = Server.MapPath("/Source/mjpjb.rar"); System.IO.FileInfo file = new System.IO ...

  9. SpringMVC实现文件下载时,请求路径中的扩展名被省略

    问题描述 问题是这样的,我写了一个DownloadController,用来处理下载请求,预期效果如下: 客户端浏览器在访问URL -->   http://localhost:8080/ssm ...

随机推荐

  1. perl数组高级

    1 去除一个数组中的重复元素: 使用grep函数代码片段: 代码: my @array = ( 'a', 'b', 'c', 'a', 'd', 1, 2, 5, 1, 5 ); my %count; ...

  2. 使用 NuGet 管理我们的程序集 - 预发行版

    1.缘起 在我们的项目中.须要引用的组件统一放在一个 Libs 文件夹下.不管对于平台上的公共组件.还是应用模块,都是如此. 假设一个应用模块,比如能源管理(EM).要引用平台提供的公共组件,比如数据 ...

  3. mormot支持https

    mormot支持https 将ssl证书导入电脑系统,以Windows 10为例: 运行 mmc 证书导入成功后,双击证书,查看证书指纹: 第二项工作:将证书与https绑定:以管理员身份启动cmd, ...

  4. ios之快速枚举

    for(UIView * subView in self.view.subviews) { if([subView isKindOfClass:[XYZSeniorQueryView class]]) ...

  5. ASIHTTPRequestErrorDomain Code=5

    ASIHttpRequest解析带空格的URL时 出错!!!(已解决) 用的是post请求 URL 地址是: http://111.234.51.56/login_member.pl?time=201 ...

  6. C#编程(四十九)----------队列

    队列 1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在Queue(队列)中的对象在一端插入,从另一端移除. 2.优点 (1).能对集合进行顺序处理( ...

  7. android开发:全屏和退出全屏

    android开发:全屏和退出全屏 from://http://blog.csdn.net/dyllove98/article/details/8831933 2013-04-21 20:31 413 ...

  8. Android 4.4 Kitkat Phone工作流程浅析(八)__Phone状态分析

    本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处 本文代码以MTK平台Android 4.4为分析对象.与Google原生AOSP有些许差异.请读者知悉. ...

  9. IIS回收后首次访问慢问题

    禁用时间间隔回收 设置为0 然后设置指定时间回收 0:00:00 然后设置脚本 @echo off @echo 正在关掉所有的IE进程(需要设置默认浏览器是IE) taskkill /im iexpl ...

  10. IE8 MIME type application/json not found

    如果: public ContentResult GetPaper(string testId) {     return ControllProctector.Do1(() =>        ...