springboot成神之——spring文件下载功能
本文介绍spring文件下载功能
目录结构

DemoApplication
package com.springlearn.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
WebConfig
package com.springlearn.learn.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")
.allowedHeaders("*");
}
}
TestController
package com.springlearn.learn.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import com.springlearn.learn.utils.MediaTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private ServletContext servletContext;
@ResponseBody
@RequestMapping(value = "/DownLoadTest", method = RequestMethod.GET)
public void Test(@RequestParam(defaultValue = "spring-boot-reference.pdf") String fileName, HttpServletResponse response) throws IOException {
MediaType mediaType = MediaTypeUtils.getMediaTypeForFileName(this.servletContext, fileName);
System.out.println("fileName: " + fileName);
System.out.println("mediaType: " + mediaType);
String filepath = "C:\\Users\\26401\\Desktop\\learn";
File file = new File(filepath + File.separator + fileName);
response.setContentType(mediaType.getType());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName());
response.setContentLength((int) file.length());
BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream outStream = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
outStream.flush();
inStream.close();
}
}
MediaTypeUtils
package com.springlearn.learn.utils;
import javax.servlet.ServletContext;
import org.springframework.http.MediaType;
public class MediaTypeUtils {
public static MediaType getMediaTypeForFileName(ServletContext servletContext, String fileName) {
String mineType = servletContext.getMimeType(fileName);
try {
MediaType mediaType = MediaType.parseMediaType(mineType);
return mediaType;
} catch (Exception e) {
return MediaType.APPLICATION_OCTET_STREAM;
}
}
}
前端测试
直接访问 http://localhost:9001/DownLoadTest 即可
springboot成神之——spring文件下载功能的更多相关文章
- springboot成神之——spring的文件上传
本文介绍spring的文件上传 目录结构 配置application DemoApplication WebConfig TestController 前端上传 本文介绍spring的文件上传 目录结 ...
- springboot成神之——spring jdbc的使用
本文介绍spring jdbc的使用 目录结构 pom配置 properties配置 model层User类 Dao层QueryForListDao config层AppConfiguration 程 ...
- springboot成神之——spring boot,spring jdbc和spring transaction的使用
本文介绍spring boot,spring jdbc和spring transaction的使用 项目结构 依赖 application model层 mapper层 dao层 exception层 ...
- springboot成神之——ioc容器(依赖注入)
springboot成神之--ioc容器(依赖注入) spring的ioc功能 文件目录结构 lang Chinese English GreetingService MyRepository MyC ...
- springboot成神之——springboot入门使用
springboot创建webservice访问mysql(使用maven) 安装 起步 spring常用命令 spring常见注释 springboot入门级使用 配置你的pom.xml文件 配置文 ...
- springboot成神之——mybatis和mybatis-generator
项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...
- springboot成神之——swagger文档自动生成工具
本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- springboot成神之——监视器
Spring Boot 的监视器 依赖 配置 书写监视控制器 常用的一些内置endpoint 定义actuator/info特殊endpoint actuator/shutdown需要post请求才能 ...
随机推荐
- C# 运行时替换方法(需要unsafe编译)
/* https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method ...
- RabbitMQ 资料整理
前言: 官方教程: https://www.rabbitmq.com/getstarted.html 应用场景(之马云赚钱): http://blog.csdn.net/whoamiyang/arti ...
- js中去除两端逗号
1.js replace(a,b)之替换字符串中所有指定字符的方法 var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str ...
- Python基础学习(第8天)
先补充些iter函数的用法:iter()其实就是一个迭代器,参数可传个list.dict等等,然后可通过调用next函数获取下一个元素,默认并未指向对象的第一个元素,可理解为指向了第一个元素的前面的位 ...
- 理解 EventLoop
链接 链接 node 浏览器 执行顺序有差异 macrotask microtask 一个线程会有 堆 栈 消息队列; 栈函数执行是用的, 堆用了存放定义的对象, 消息队列来处理异步的操作 a() ...
- delete和truncate区别
相同之处:truncate在功能上与不带WHERE子句的delete 语句相同:二者均删除表中的全部行.小心使用truncate,删除后就没有了 1.delete : 删除"表格记录&quo ...
- Illumina Fastq Q-score
Illumina Nextseq500 Miseq HiseqXten 测序仪 Q-score均采用下面的编码格式,仅作简要介绍. Q-score Q-score 在fastq中每个序列的第4行,代表 ...
- 手把手教你怎么用ArcgisOnline发布地图服务
Arcgis推出了Arcgis Online,但是大家都不知道这是个什么东西,怎么用这个东西,今天这篇文章手把手的教你如何使用Arcgisonline发布地图服务. 一.ArcgisOnline简介 ...
- vue前端开发那些事——vue开发遇到的问题
vue web开发并不是孤立的.它需要众多插件的配合以及其它js框架的支持.本篇想把vue web开发的一些问题,拿出来讨论下. 1.web界面采用哪个UI框架?项目中引用了layui框架.引入框架 ...
- vi下的查找替换命令
1.查找 查找命令 /pattern:向下查找匹配的字符 ?pattern:向上查找匹配的字符 其中pattern是需要匹配的字符串,例如: /cd #全文查找cd / cd <Enter> ...