Springboot文件下载代码
来源:互联网 发布:怎么提升淘宝买家等级 编辑:程序博客网 时间:2024/05/20 00:52
该案例简单实用,分享给大家:
直接上代码
@RequestMapping("/excel")public ResponseEntity<InputStreamResource> downFile(Long id) throws IOException{System.out.println("开始下载文件了");String filepath = "F:/aa.xlsx";FileSystemResource file = new FileSystemResource(filepath);HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.add("Cache-Control", "no-cache,no-store,must-revalidate");httpHeaders.add("Content-Disposition", String.format("attachment;filename=\"%s\"", file.getFilename()));httpHeaders.add("Pragma", "no-cache");httpHeaders.add("Expires", "0");return ResponseEntity.ok().headers(httpHeaders).contentLength(file.contentLength()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new InputStreamResource(file.getInputStream()));}@SuppressWarnings("resource")@RequestMapping("/image")public void getDownload(Long id,HttpServletRequest request,HttpServletResponse response){System.out.println("开始下载图片了");String fullPath = "F:/aa.jpg";File downloadFile = new File(fullPath);ServletContext context = request.getServletContext();//获取MIMI类型String mimeType = context.getMimeType(fullPath);if(mimeType == null){//设置二进制类型如果该文件的MIME类型没获取到mimeType = "application/octet-stream";System.out.println("context getMimeType is null");}System.out.println("MIME Type: "+mimeType);response.setContentType(mimeType);response.setContentLength((int) downloadFile.length());//设置响应头String headerKey = "Content-Disposition";String headerValue = String.format("attachment;filename=\"%s\"", downloadFile.getName());response.setHeader(headerKey, headerValue);//复制流,完成复制try {FileInputStream myStream = new FileInputStream(downloadFile);IOUtils.copy(myStream, response.getOutputStream());response.flushBuffer();} catch (Exception e) {System.out.println("复制图片报异常了");e.printStackTrace();}}
0 0
- Springboot文件下载代码
- Springboot文件下载
- SpringBoot文件下载
- Springboot文件下载
- SpringBoot之文件下载
- SpringBoot下载文件
- SpringBoot的文件下载
- springboot+文件上传和下载
- SpringBoot SpringMVC实现文件下载
- SpringBoot文件上传和下载
- SPRINGBOOT之文件上传与下载
- 下载文件的代码
- WebClient下载文件代码!
- asp 文件下载代码
- ASP文件下载代码
- php下载文件代码
- javascript文件下载代码
- 文件下载代码
- static 、final区别
- Android中自定义Shape小结
- Fragment与Fragment、Activity通信(2)
- nginx+keeplived实现高可用站点主备模式搭建
- linux 6转换系统审计日志文件
- Springboot文件下载代码
- SVM 中 rbf kernel 的意义 —— 一个不怎么严谨的解释
- Ildasm.exe(IL 反汇编程序)
- Logger的使用
- 时间的函数Sleep,clock,GetTickCount,QueryPerformanceCounter
- MATLAB学习笔记之-----数列的循环移位操作
- React Native ES6写法整理
- prim算法
- 题解:DNA序列(UVa 1368)