SpringBoot/SpringMVC文件下载方式

时间:2023-02-20 18:45:06

本篇文章引用外网博客代码,共描述SpringMVC下三种文件下载方式,本人测试在SpringBoot(2.0以上版本)正常使用.

引用博客,强烈推荐https://www.boraji.com.

package com.boraji.tutorial.spring.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import javax.servlet.http.HttpServletResponse; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; /**
* @author imssbora
*/
@Controller
public class FileDownloadController { private static final String FILE_PATH = "D:/e-Books/jsp_tutorial.pdf"; @GetMapping("/")
public String fileUploadForm(Model model) {
return "fileDownloadView";
} // Using ResponseEntity<InputStreamResource>
@GetMapping("/download1")
public ResponseEntity<InputStreamResource> downloadFile1() throws IOException { File file = new File(FILE_PATH);
InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=" + file.getName())
.contentType(MediaType.APPLICATION_PDF).contentLength(file.length())
.body(resource);
} // Using ResponseEntity<ByteArrayResource>
@GetMapping("/download2")
public ResponseEntity<ByteArrayResource> downloadFile2() throws IOException { Path path = Paths.get(FILE_PATH);
byte[] data = Files.readAllBytes(path);
ByteArrayResource resource = new ByteArrayResource(data); return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=" + path.getFileName().toString())
.contentType(MediaType.APPLICATION_PDF).contentLength(data.length)
.body(resource);
} // Using HttpServletResponse
@GetMapping("/download3")
public void downloadFile3(HttpServletResponse resonse) throws IOException {
File file = new File(FILE_PATH); // 示例中使用的是pdf,实际的content-type需要根据上传文件时的content-type进行确定(最长可达255字节)
resonse.setContentType("application/pdf");
resonse.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
BufferedInputStream inStrem = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream outStream = new BufferedOutputStream(resonse.getOutputStream()); byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inStrem.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
outStream.flush();
inStrem.close();
}
}

ResponseEntity表示HTTP响应,包含body,header以及status code.

ResponseEntity可以在RestTemplate 以及@Controller中使用.

PS:

如果您觉得我的文章对您有帮助,可以扫码领取下红包或扫码支持(随意多少,一分钱都是爱),谢谢!

支付宝红包 支付宝 微信
SpringBoot/SpringMVC文件下载方式 SpringBoot/SpringMVC文件下载方式 SpringBoot/SpringMVC文件下载方式

SpringBoot/SpringMVC文件下载方式的更多相关文章

  1. springboot&plus;springmvc&plus;mybatis项目整合

    介绍: 上篇给大家介绍了ssm多模块项目的搭建,在搭建过程中spring整合springmvc和mybatis时会有很多的东西需要我们进行配置,这样不仅浪费了时间,也比较容易出错,由于这样问题的产生, ...

  2. SpringBoot&plus;SpringMVC&plus;MyBatis快速整合搭建

    作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Spr ...

  3. springboot&plus;springmvc拦截器做登录拦截

    springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration ...

  4. 腾讯公司数据分析岗位的hadoop工作 线性回归 k-means算法 朴素贝叶斯算法 SpringMVC组件 某公司的广告投放系统 KNN算法 社交网络模型 SpringMVC注解方式

    腾讯公司数据分析岗位的hadoop工作 线性回归 k-means算法 朴素贝叶斯算法 SpringMVC组件 某公司的广告投放系统 KNN算法 社交网络模型 SpringMVC注解方式 某移动公司实时 ...

  5. springMVC注解方式&plus;easyUI&plus;MYSQL配置实例

    刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+e ...

  6. SpringBoot的文件下载

    SpringBoot的文件下载 2017年11月29日 10:32:20 阅读数:3907 SpringBoot的文件下载方法有很多,此处只记录使用Spring的Resource实现类FileSyst ...

  7. Java 系列之spring学习--springmvc注解方式(五)

    一.springmvc注解方式 注解方式使用的更多,更加灵活.在上一篇的博客的基础上修改springmvc-servlet.xml配置文件. <?xml version="1.0&qu ...

  8. 前端基于vue&comma;后台采用springboot&plus;shiro的方式搭建的一个移动端商品展示平台

    基于vue实现的移动端商品展示页,可以web-view的方式嵌入到小程序中,布局简约.大气,减少初学者或开发者不必要的工作量.后台维护采用的springboot+shiro的方式,为广大爱好者提供展示 ...

  9. linux环境下,springboot jar启动方式

    linux环境下,springboot jar启动方式 一.前台启动(ctrl+c会关掉进程) java -jar application.jar 二.后台启动(ctrl+c不会关闭) java -j ...

随机推荐

  1. The connection to adb is down&comma; and a severe error has occured&period;问题解决方法小结

    遇到了几次这个问题:The connection to adb is down, and a severe error has occured. You must restart adb and Ec ...

  2. 【JAVA】final修饰Field

    一.简介 final修饰符可以用来修饰变量.方法.类.final修饰变量时一旦被赋值就不可以改变. 二.final成员变量 成员变量是随类初始化或对象初始化而初始化的.当类初始化的时候,会给类变量分配 ...

  3. 九度OJ 1348 数组中的逆序对 -- 归并排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1348 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求 ...

  4. org&period;springframework&period;web&period;servlet&period;view&period;InternalResourceViewResolver

    http://blog.csdn.net/superdog007/article/details/28857495 我们在controller里面经常这样return一个ModelAndView: r ...

  5. EXT JS 4&period;3 在线学习

    官网地址:http://docs.sencha.com/extjs/4.1.3/ 相关示例:http://docs.sencha.com/extjs/4.1.3/#!/example Examples ...

  6. Android studio教程:&lbrack;2&rsqb;项目整体布局

    上篇介绍了如何创建项目,这一次将介绍创建完的项目如何呈现在开发者的眼前,介绍android studio开发环境的整体布局,让大家知道各个模块的位置和功能. 工具/原料 Android studio ...

  7. 持续集成:TestNG组织如何测试用例

    持续集成:TestNG组织如何测试用例   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90 ...

  8. 2014新年福利,居然有人将Ext JS 4&period;1的文档翻译了

    原文:http://damoqiongqiu.iteye.com/blog/1998022

  9. MySQL5&period;7使用错误解决:ERROR 1045 &lpar;28000&rpar;&colon; Access denied for user &&num;39&semi;root&&num;39&semi;&commat;&&num;39&semi;localhost&&num;39&semi; &lpar;using password&colon; NO&rpar;【取消或重设root密码】

    解决方法: 最简单方法: ⑴打开mysql中的my.ini(如果没有就将my-default.ini复制一份,并修改为my.ini): ⑵在[mysqld]下面空白行直接添加skip-grant-ta ...

  10. python入门知识点(下)

    51.函数的文档注释及作用 """ 函数的文档注释: 函数的注释文字. 必须添加到函数定义的那一行的下面一行. 好处: 当使用Ctrl + Q查看函数的使用说明文档时,能 ...