本文介绍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 即可

最新文章

  1. [nodemon] Internal watch failed: watch ENOSPC错误解决办法
  2. javascript设置和获取cookie的通用方法
  3. html-制作导航菜单
  4. no drawer view found with gravity RIGHT(Android实现侧滑菜单从右面滑出) 解决办法
  5. C程序设计语言练习题1-19
  6. sharePoint常用命令
  7. AFHTTPSessionManager
  8. java基础(5)----面向对象
  9. Chrome启动后打开第一个网页很慢的解决方案
  10. vue每次修改刷新当前子组件
  11. LeetCode--No.009 Palindrome Number
  12. SQLite保存报错sqlite.SQLiteConstraintException: UNIQUE constraint failed: ······ code 1555
  13. PAT-L2-006(根据后序中序遍历建立树)
  14. GDI+绘制渐变色
  15. UVALive 6257 Chemist's vows
  16. biicode:一个现代的 C 依赖管理器
  17. feignClient中修改ribbon的配置
  18. easyui_extension.js
  19. Druid.io系列(三): Druid集群节点
  20. HTML常用标签用法及实例

热门文章

  1. Tavas and Karafs 二分+结论
  2. quartz简单应用
  3. 《转》浅谈EJB
  4. 不用配置 , 快速搭建react环境
  5. Android onTouchEvent和setOnTouchListener中onTouch的区别
  6. Android 进阶10:进程通信之 Messenger 使用与解析
  7. ng 变量和常量服务
  8. Mongodb 的劣势
  9. OpenCV中阈值(threshold)函数: threshold 。
  10. LeetCode — (1)