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 ...
随机推荐
- python测试开发django-16.JsonResponse返回中文编码问题
前言 django查询到的结果,用JsonResponse返回在页面上显示类似于\u4e2d\u6587 ,注意这个不叫乱码,这个是unicode编码,python3默认返回的编码 遇到问题 接着前面 ...
- POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路
累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...
- Thinking in Java---异常处理机制
java的异常处理机制能够使程序有极好的容错性,让程序更加的健壮.所谓的异常,就是指的阻止当前方法或作用域继续运行的问题,,当程序运行时出现异常时,系统就会自己主动生成一个Exception对象来通知 ...
- PL/SQL 存储过程
PL/SQL复习九 存储过程 无参数的存储过程: create or replace procedure out_time is begin dbms_output.put_line(to_char( ...
- ifram 取父窗体的URL地址
var url=''; try { url = window.top.document.referrer ; } catch(M) { if (window.parent) ...
- 使用SpringBoot的关于页面跳转的问题
示例如下: @Controller public class UserController { @Resource UserService userService; @RequestMapping(& ...
- [Web 前端] CSS篇之 4. position 和 display 的取值和各自的意思和用法
讲一讲CSS的position/float/display都有哪些取值,它们相互叠加时的行为都是什么? 列出display的值,说明他们的作用.position的值, relative和absolu ...
- SVG.js 基础图形绘制整理(一)
一.矩形 //指定width和height 画矩形 //返回rect对象 var draw = SVG('svg1').size(300, 300); var rect = draw.rect(100 ...
- 伯努利分布、二项分布、Beta分布、多项分布和Dirichlet分布与他们之间的关系,以及在LDA中的应用
在看LDA的时候,遇到的数学公式分布有些多,因此在这里总结一下思路. 一.伯努利试验.伯努利过程与伯努利分布 先说一下什么是伯努利试验: 维基百科伯努利试验中: 伯努利试验(Bernoulli tri ...
- [转]PHP中替换换行符
FROM :http://www.cnblogs.com/siqi/archive/2012/10/12/2720713.html //php 有三种方法来解决 //1.使用str_replace 来 ...