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不是太难,学习成本不高,现 ...
随机推荐
- java.io.ByteArrayOutputStream 源码分析
ByteArrayOutputStream 内部包含了一个缓冲区,缓冲区会随着数据的不断写入而自动增长,俗称内存流. 首先看一下俩个属性,buf是内部缓冲区,count是记录写入了多少个字节. pro ...
- PowerDesigner使用教程【转】
PowerDesigner使用教程 —— 概念数据模型 一.概念数据模型概述 概念数据模型也称信息模型,它以实体-联系(Entity-RelationShip,简称E-R)理论为基础,并对这 ...
- UEFI Protocol
MEM_INFO_PROTOCOL MEM_INFO_PROTOCOL; EFI_LOADED_IMAGE_PROTOCOL EFI_DEVICE_PATH_PROTOCOL EFI_DRIVER_B ...
- 【进阶修炼】——改善C#程序质量(10)
158,不要写冗余注释. 注释应该写代码没有表达的东西. 代码能够自我描述就不要加注释. 159,废弃的注释应该尽早删除. 废弃的注释由于年代太久远,已经和现在的代码逻辑不匹配了,这样的注释只会误导人 ...
- Ansible 进阶技巧
原文 http://www.ibm.com/developerworks/cn/linux/1608_lih_ansible/index.html?ca=drs- 简介 Ansible 是一个系 ...
- Spring Cloud 关于 hystrix 的异常 fallback method wasn't found
在 Spring Cloud 中使用断路器 hystrix 后,可能会遇到异常:com.netflix.hystrix.contrib.javanica.exception.FallbackDefin ...
- Android 自定义倒计时控件CountdownTextView
效果如下: 剩余 00:59:21 package com.bihu.advertiserapp.widgets; import android.annotation.TargetApi; impor ...
- [Intellij] Project Structure 配置说明
IntelliJ IDEA 的Project structure可以在File->Project structure中打开,同时,在新建项目是IDE一般用向导的方式让你填写Project str ...
- SharePoint 2013 Support for Windows Server 2012 R2
Summary Currently, Microsoft SharePoint Server 2013 is not supported for installation on computers r ...
- ssh : how to add "hostkey" to “know_hosts”
有时后端daemon或者脚本在执行ssh连接时,会遇到以下提示: The authenticity of host 'git.sws.com (10.42.1.88)' can't be establ ...