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

参考

上一篇:CAM350 - 导入光绘(GERBER)文件
下一篇:Spring Boot - axios upload file(带请求头上传文件,非前后端分离)

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年03月10日 06时43分25秒