Spring MVC 特性实现文件下载
代码
import java.io.File;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;import com.srieservice.dell.ApplicationProperties;@Controller
@RequestMapping("/app/download")
public class DownloadController {private Logger log = LoggerFactory.getLogger(this.getClass());@Autowiredprivate ApplicationProperties appProperties;@RequestMapping(path= {"", "index"}, method=RequestMethod.GET)public ResponseEntity<byte[]> index(HttpSession session, @RequestParam("filename")String filename) {// 检查一下是否有下载权限?// 组合文件File downloadFile = new File(this.appProperties.getDocumentFolder(), filename);if (downloadFile.exists()){log.debug("download file : {}", downloadFile.getAbsoluteFile());} else {throw new RuntimeException("文件不存在!");}// 组合 Header,确定 ContentType 和文件名HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", filename); // 构造 ResponseEntityreturn new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(downloadFile), headers, HttpStatus.CREATED);}
}
Spring MVC 特性实现文件下载相关推荐
- zbb20180613 Spring MVC实现大文件下载功能
Spring MVC实现大文件下载功能 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...
- Spring Boot特性
2019独角兽企业重金招聘Python工程师标准>>> SpringApplication SpringApplication类提供了一种从main()方法启动Spring应用的便捷 ...
- [转] Spring Boot特性
[From] http://blog.javachen.com/2015/03/13/some-spring-boot-features.html 1. SpringApplication Sprin ...
- Eclipse Maven构建Spring MVC项目
工作中项目开发使用Maven管理项目的构建.打包.编译,框架採用的是Spring MVC框架,而且实现了多模块.多项目的管理.自己也简单的參与了架构的设计.对于刚開始学习的人来说,使用Maven构建项 ...
- Spring 2.5:Spring MVC中的新特性
转载说明:infoQ就是牛人多,看人家去年就把Spring2.5注视驱动的MVC写出来了,还是这么详细,我真是自叹不如,今天偶尔看到这篇文章非常认真的拜读了2遍,简直是茅厕顿开啊....\(^o^)/ ...
- Spring4.1新特性——Spring MVC增强
2019独角兽企业重金招聘Python工程师标准>>> 1.GroovyWebApplicationContext 在Spring 4.1之前没有提供Web集成的Applicati ...
- Spring MVC 文件上传 文件下载
索引: 目录索引 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadController.java Files_Ut ...
- Spring MVC中的视图解析ViewResolver
http://blog.csdn.net/prince2270/article/details/5891085 在Spring MVC中,当Controller将请求处理结果放入到ModelAndVi ...
- Spring MVC框架——Web开发框架
1.MVC设计模式 一般指MVC框架,M(Model)数据模型层,V(View)视图层,C(Controller)控制层. Controller层:获取用户的请求,处理相关业务逻辑,并成功跳转页面,负 ...
最新文章
- SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
- 只要你的AI算法能比小白鼠聪明,DeepMind的这20万奖金请拿走
- DeepLearning based on PaddlePaddle系列二
- UE4 使用VaRest的最佳实践
- AUTH password
- mysql linux root密码忘记了怎么办,linux下忘记mysql的root密码解决办法 | 严佳冬
- UVA12043 Divisors【欧拉筛法】
- 解决 U盘安装Windows Server 2012 R2 报错
- linux 下载git源码,在linux系统下Git源码系统的文件下载
- 使用ffmpeg+opencv播放ts格式的视频
- lua学习03:tolua的编译和使用:C/C++调用lua、lua调用C++、lua调用tolua、常用tolua和lua的API介绍
- 动作捕捉 Motion Capture (Mocap)
- 11.3 帧中继基本配置
- First集和Follow集的求法
- 计算机excel中钱的符号,在excel中输入钱的符号
- 编程学习应用 Growth 发布 3.0,在碎片时间里练习
- 《汇编语言程序设计》(双语)上机指导
- Discuz!电脑版、手机版去除标题title中的Powered by Discuz!
- 寒風的Cocos2dx之旅之剪刀、石头、布系列专题(2 )
- 搜狗输入法怎么输入上下标