springmvc 文件下载
1、使用servlet的API实现
参考:http://my.oschina.net/u/1394615/blog/311307
@RequestMapping("/download")
public String download(String fileName, HttpServletRequest request,
HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName="
+ fileName);
try {
String path = Thread.currentThread().getContextClassLoader()
.getResource("").getPath()
+ "download";//这个download目录为啥建立在classes下的
InputStream inputStream = new FileInputStream(new File(path
+ File.separator + fileName));
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
// 这里主要关闭。
os.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 返回值要注意,要不然就出现下面这句错误!
//java+getOutputStream() has already been called for this response
return null;
}
2、使用spring的API实现
参考:http://blog.csdn.net/clj198606061111/article/details/20743769
package com.clj.test.down.util; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping; /**
* <一句话功能简述>
* <功能详细描述>
*
* @author Administrator
* @version [版本号, 2014年3月7日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@Component
@Scope("prototype")
@RequestMapping("/downloadFile")
public class DownloadAction
{ @RequestMapping("download")
public ResponseEntity<byte[]> download() throws IOException {
String path="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\springMVC\\WEB-INF\\upload\\图片10(定价后).xlsx";
File file=new File(path);
HttpHeaders headers = new HttpHeaders();
String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
}
}
打发大丰
springmvc 文件下载的更多相关文章
- SpringBoot/SpringMVC文件下载方式
本篇文章引用外网博客代码,共描述SpringMVC下三种文件下载方式,本人测试在SpringBoot(2.0以上版本)正常使用. 引用博客,强烈推荐https://www.boraji.com. pa ...
- SpringMVC文件下载与JSON格式
点击查看上一章 现在JSON这种数据格式是被使用的非常的广泛的,SpringMVC作为目前最受欢迎的框架,它对JSON这种数据格式提供了非常友好的支持,可以说是简单到爆. 在我们SpringMVC中只 ...
- SpringMvc 文件下载 详解
最近SSM 需要用到文件下载,以前没用过,在百度上找了好久发现没有一篇博客,对于此段代码进行详细讲解, 这里是本人的个人总结,跟大家分享一下!!!不谢 /** * 文件下载 * ResponseEnt ...
- springMvc文件下载
//主要看导入的是那些类 import com.ibm.db.service.ITopicService;import org.apache.commons.io.FileUtils;import o ...
- springmvc文件下载之文件名下划线问题终极解决方案
直接上代码:Action中代码片段. @RequestMapping("download")public String download(ModelMap model, @Mode ...
- springmvc 文件下载分批读取,防止内存溢出
参考 https://blog.csdn.net/u014732956/article/details/51404086
- SpringMVC:学习笔记(9)——文件下载
SpringMVC—文件下载 说明 两个案例 1.为登录用户提供下载服务. 2.阻止仅通过输入网址即可获取下载. 文件下载概览 为了将文件发送给浏览器,我们需要在控制器中完成以下操作: 对请求处理方法 ...
- 基于 Nginx XSendfile + SpringMVC 进行文件下载
转自:http://denger.iteye.com/blog/1014066 基于 Nginx XSendfile + SpringMVC 进行文件下载 PS:经过实际测试,通过 nginx 提供文 ...
- SpringMVC最基础配置
SpringMVC和Struts2一样,是前后台的一个粘合剂,struts2用得比较熟悉了,现在来配置一下SpringMVC,看看其最基础配置和基本使用.SpriingMVC不是太难,学习成本不高,现 ...
随机推荐
- 关于C3P0容错和自动重连特性的研究
转载: http://blog.csdn.net/cutesource/article/details/5422093 最近常有数据库和网络设备升级和搬迁等事情,而各个应用都是基于数据库连接池做的,大 ...
- ListView@常用属性记录
android:stackFromBottom="true" | "false" 默认false 说明:当listview加载完毕,显示最下面的内容,或者显示最 ...
- c# 自定义类型的DataBindings
自定义类型TextBoxEx,扩展了TextBox,增加了一个属性KeyText来保存后台的值(Tag已另作它用). 程序里面需要将KeyText和DataTable的某个列绑定起来. 如果是Text ...
- iOS应用跳转到App Store评分
iOS应用跳转到App Store评分 1.跳转到应用评价页 NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itun ...
- jQuery(八):属性操作
一.获取或设置元素的属性值 attr()获取或设置匹配元素的属性值,语法如下: 获取元素属性值示例: <!DOCTYPE html> <html lang="en" ...
- PHP开发安全问题总结
php给了开发者极大的灵活性,但是这也为安全问题带来了潜在的隐患,近期需要总结一下以往的问题,在这里借翻译一篇文章同时加上自己开发的一些感触总结一下. 简介 当开发一个互联网服务的时候,必须时刻牢记安 ...
- FPGA中的时序分析(二)
使用Timequest 笔者对Altera较熟悉,这里以quartus ii中的timequest作为讲解. Timequest分析时序的核心,也就是在于延迟因数的计算.那么建立约束文件,去告诉tim ...
- php命令行脚本 mock数据
<?php $con = mysql_connect("192.168.1.5:3306","root","123"); if (!$ ...
- IDEA 在某个工程下一个module如何使用另一个module中的资源文件(.xml .prop等)
问题如题,经google,解决方案有四种,选择了比较直观有效的一种罗列如下: 因为项目采用maven管理,所以我们可以在module2下的pom.xml制定<resources>的路径,让 ...
- error: navicat 连接debian系列系统mysql 10038问题解决方案
还特么有一种可能 阿里云 也有防火墙出口入口规则,这里也有问题