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不是太难,学习成本不高,现 ... 
随机推荐
- [转] 怎么减少编程中的 bug?
			[转]http://macshuo.com/?p=1361 怎么减少编程中的 bug? Posted on 2016 年 2 月 17 日 为什么要编程?因为代码没在那里.创造一个世界是如此让人着迷, ... 
- Chrome 调用vue.js 记录
			一.今晚遇到一个问题,就是不能直接在 html 直接调用 js 代码的问题 二.会出现如下错误: Refused to execute inline script because it violate ... 
- docker原理
			Docker原理11 Linux Namespace 11 AUFS文件系统17 重新理解Docker的各种命令18 Docker原理 Linux Namespace docker是一个容器引擎,容器 ... 
- Redis (1) —— 安装
			Redis (1) -- 安装 摘要 介绍Mac OS X安装Redis基本方法 版本 Redis版本: 2.8.24 内容 下载Redis包 地址:http://download.redis.io/ ... 
- webstorm 设置jsp支持ZenCoding
			setting -> File Type - html 里面增加*.jsp 
- input 数字输入控制(含小数)
			1,可以在keypress里验证输入的字符的合法性. // 数字控件的入力控制 $("input.comma,input.comma1,input.comma2").keypres ... 
- Linux排序命令sort详解
			语法格式sort [ -A ] [ -b ] [ -c ] [ -d ] [ -f ] [ -i ] [ -m] [ -n ] [ -r ] [ -u ] [ -o OutFile ][ -t Cha ... 
- vbs中的"WScript.Network"[属性与方法]
			属性ComputerName 计算机名UserDomain 所属局域网域的域名UserName ... 
- ggplot2 subscript in x-axis labels(ticks, legend)
			#==============================# ggplot2: subscript in x-axis labels(ticks) rm(list=ls(all=TRUE))lib ... 
- Socket心跳包机制
			心跳包的发送,通常有两种技术方法1:应用层自己实现的心跳包 由应用程序自己发送心跳包来检测连接是否正常,大致的方法是:服务器在一个 Timer事件中定时 向客户端发送一个短小精悍的数据包,然后启动一个 ... 
