
Spring Boot - axios download file(带请求头下载、预览 PDF 文件,非前后端分离)
发布日期:2021-05-06 23:03:05
浏览次数:17
分类:技术文章
本文共 3357 字,大约阅读时间需要 11 分钟。
文章目录

项目
新建 Spring Starter Project,编辑 pom.xml 文件,引入依赖:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.mk spring-boot-axios-download-file 1.0.0 spring-boot-axios-download-file 1.8 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true commons-io commons-io 2.6 org.springframework.boot spring-boot-maven-plugin org.springframework.boot spring-boot-configuration-processor org.projectlombok lombok
IndexController
控制器:
package com.mk.controller;import java.io.File;import java.io.IOException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.io.FileUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;@Controllerpublic class IndexController { @GetMapping({ "", "/index"}) public String index() { return "index"; } @GetMapping("/download") public void download(HttpServletRequest request, HttpServletResponse response) throws IOException { String authorization = request.getHeader("Authorization"); System.out.println(authorization); byte[] bytes = FileUtils.readFileToByteArray(new File("E:/stm32/w25q128fv rev.m 05132016 kms.pdf")); ServletOutputStream os = response.getOutputStream(); response.setHeader("Content-Type", "application/pdf"); os.write(bytes); os.flush(); os.close(); }}
启动类:
package com.mk;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
src/main/resources/templates/index.html 文件:
Download File
参考
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年03月10日 06时43分25秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【redis键过期删除策略】很高兴再次认识你
2019-03-03
【工具篇】EasyExcel的应用
2019-03-03
SSM发送手机验证码——以网建SMS为例
2019-03-03
大范围卫星影像快速处理
2019-03-03
监控264后缀文件播放
2019-03-03
网站在线偷拍照片源码
2019-03-03
动态摇动吊牌自适应网站404页面源码
2019-03-03
炫酷文字消失动画网站404页面源码
2019-03-03
EMLOG模板山河网站主题分享
2019-03-03
2019数字音乐市场年度回顾,QQ音乐全面领先
2019-03-03
花1亿扶持优质红人,如涵推动网红经济出圈之路有何深意?
2019-03-03
抢滩抖音、B站,快手港股IPO进程加速
2019-03-03
Linux中的虚拟内存机制和内存映射
2019-03-03
Android系统启动系列5 SystemServer进程下
2019-03-03
Android四大组件系列9 ContentProvider原理
2019-03-03
理解PendingIntent
2019-03-03
Android SurfaceFlinger4 提交Buffer
2019-03-03
深入理解 ClientLifecycleManager 机制
2019-03-03
android基础知识回顾--ContentProvider简单用法
2019-03-03
压缩解压
2019-03-03