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 文件下载的更多相关文章

  1. SpringBoot/SpringMVC文件下载方式

    本篇文章引用外网博客代码,共描述SpringMVC下三种文件下载方式,本人测试在SpringBoot(2.0以上版本)正常使用. 引用博客,强烈推荐https://www.boraji.com. pa ...

  2. SpringMVC文件下载与JSON格式

    点击查看上一章 现在JSON这种数据格式是被使用的非常的广泛的,SpringMVC作为目前最受欢迎的框架,它对JSON这种数据格式提供了非常友好的支持,可以说是简单到爆. 在我们SpringMVC中只 ...

  3. SpringMvc 文件下载 详解

    最近SSM 需要用到文件下载,以前没用过,在百度上找了好久发现没有一篇博客,对于此段代码进行详细讲解, 这里是本人的个人总结,跟大家分享一下!!!不谢 /** * 文件下载 * ResponseEnt ...

  4. springMvc文件下载

    //主要看导入的是那些类 import com.ibm.db.service.ITopicService;import org.apache.commons.io.FileUtils;import o ...

  5. springmvc文件下载之文件名下划线问题终极解决方案

    直接上代码:Action中代码片段. @RequestMapping("download")public String download(ModelMap model, @Mode ...

  6. springmvc 文件下载分批读取,防止内存溢出

    参考 https://blog.csdn.net/u014732956/article/details/51404086    

  7. SpringMVC:学习笔记(9)——文件下载

    SpringMVC—文件下载 说明 两个案例 1.为登录用户提供下载服务. 2.阻止仅通过输入网址即可获取下载. 文件下载概览 为了将文件发送给浏览器,我们需要在控制器中完成以下操作: 对请求处理方法 ...

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

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

  9. SpringMVC最基础配置

    SpringMVC和Struts2一样,是前后台的一个粘合剂,struts2用得比较熟悉了,现在来配置一下SpringMVC,看看其最基础配置和基本使用.SpriingMVC不是太难,学习成本不高,现 ...

随机推荐

  1. EhCache 配置信息

    How to Size Caches 官方文档:http://ehcache.org/documentation/configuration/cache-size [maxEntriesLocalHe ...

  2. 如何在CentOS 6.5上升级PHP

    CentOS 6.5上默认安装PHP 5.3.因为后台网站无法正确运行在PHP 5.3上,所以计划将PHP升级到开发平台一样的版本PHP 5.5.为了方便,我们采用YUM的方式升级PHP 工具/原料 ...

  3. android开发(33) 让 actionbar 透明2

    让 actionbar 的背景 透明 我需要一个 透明的actionbar ,找到如下方法实现: 1. 首先,设置ActionBar 浮动到主界面上来. 2. 然后,设置ActionBar的背景色,透 ...

  4. PCL特征点与配准(1)

    关于输入一个具体的物体的点云,从场景中找出与该物体点云相匹配的,这种方法可以用来抓取指定的物体等等,具体的代码的解释如下,需要用到的一些基础的知识,在之前的博客中都有提及,其中用到的一些方法可以翻阅前 ...

  5. 【转】常用 Microsoft .NET Framework 各版本下載網址列表

    研究] 常用 Microsoft .NET Framework 各版本下載網址列表 2014-05-23 僅列常用的 (IA64, Beta, hotfix, ... 不列) Microsoft .N ...

  6. 工行金邦达USBKey 在Mac OS的 Parallels Desktop 的虚拟Win7出现“ PKCS11

    由于经常用到支付宝的工行付款,所以在我的mac os中安装了Parallels Desktop 7(Win7旗舰版),但随之而来的问题是,运行PD后,再插入工行的USBkey,运行工具箱后出现“PKC ...

  7. FileOutPutStream in 创新实训 自然语言交流系统

    FileOutPutStream在c盘等一级目录下是可以创建文件的,如: new FileOutputStream("c:\\kk.txt");但是在c\\test等就创建不了,F ...

  8. 关于Unity中网格导航与寻路

    寻路思路 1.烘焙出地形数据,导航数据,区分哪些是路径,哪些是障碍物 2.给要寻路的角色添加寻路的组件,加好了以后就会有速度和目的地之类的参数设置 3.只要设置好目的地,角色就会根据烘焙好的地图自己走 ...

  9. 【JavaScript学习】JavaScript对象创建

    1.最简单的方法,创建一个对象,然后添加属性 var person = new Object(); person.age = 23; person.name = "David"; ...

  10. Ogre2.0 全新功能打造新3D引擎

    不知当初是在那看到,说是Ogre2.0浪费了一个版本号,当时也没多想,以为没多大更新,一直到现在想做一个编辑器时,忽然想到要看下最新版本的更新,不看不知道,一看吓一跳,所以说,网络上的话少信,你不认识 ...