目录

  • 项目
  • 参考


项目

新建 Spring Starter Project,编辑 pom.xml 文件,引入依赖:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>        <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.3.3.RELEASE</version>        <relativePath /> <!-- lookup parent from repository -->    </parent>        <groupId>com.mk</groupId>    <artifactId>spring-boot-axios-download-file</artifactId>    <version>1.0.0</version>        <name>spring-boot-axios-download-file</name>        <properties>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <scope>runtime</scope>            <optional>true</optional>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-configuration-processor</artifactId>            <optional>true</optional>        </dependency>        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <optional>true</optional>        </dependency>        <!-- Commons IO -->        <dependency>            <groupId>commons-io</groupId>            <artifactId>commons-io</artifactId>            <version>2.6</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <configuration>                    <excludes>                        <exclude>                            <groupId>org.springframework.boot</groupId>                            <artifactId>spring-boot-configuration-processor</artifactId>                        </exclude>                        <exclude>                            <groupId>org.projectlombok</groupId>                            <artifactId>lombok</artifactId>                        </exclude>                    </excludes>                </configuration>            </plugin>        </plugins>    </build></project>

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 文件:

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <title>Download File</title>    </head>    <body>        <input type="button" id="download" value="download" />        <input type="button" id="preview" value="preview" />        <br />        <iframe width="100%" height="500"></iframe>        <script type="text/javascript" src="/js/axios.min.js"></script>        <script type="text/javascript">            window.onload = (event) => {                const downloadButton = document.getElementById('download')                const previewButton = document.getElementById('preview')                const iframe = document.querySelector('iframe')                                downloadButton.onclick = function(event) {                    const url = '/download'                    const method = 'GET'                    const headers = { 'Authorization': '1234567890' }                    const responseType = 'blob'                    axios({                        url, method, headers, responseType                    }).then(response => {                        console.log(response)                                                const blob = response.data                        const objectURL = URL.createObjectURL(blob)                                                                            const a = document.createElement('a')                        a.href = objectURL                        a.download = "123.pdf"                        a.click()                                                URL.revokeObjectURL(objectURL)                    }).catch(error => console.error(error))                }                                previewButton.onclick = function(event) {                    const url = '/download'                    const method = 'GET'                    const headers = { 'Authorization': '1234567890' }                    const responseType = 'blob'                    axios({                        url, method, headers, responseType                    }).then(response => {                        console.log(response)                                                const blob = response.data                        const objectURL = URL.createObjectURL(blob)                                                iframe.setAttribute('src', objectURL)                                                                            URL.revokeObjectURL(objectURL)                    }).catch(error => console.error(error))                }            }        </script>    </body></html>


©著作权归作者所有:来自51CTO博客作者mb6066e453ca35b的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. Spring Boot - axios upload file(带请求头上传文件,非前后端分离)
  2. 优雅spring-boot项目启动,停止,重启脚本
  3. Spring Boot - XMLHttpRequest Upload(上传文件,非前后端分离)
  4. 优盘文件或目录损坏且无法读取怎么办 ,文件或目录损坏且无法读取
  5. 第二周(第十天)练习题 2018年4月1日 16:45:20
  6. rsync服务
  7. 其他空间类的引用,别名引用,自动加载类,数据库的简单操作
  8. 磁盘空间空间不足的原因及表现与解决办法
  9. PDF Password Remover 软件及其密钥

随机推荐

  1. Android 圆角图片,基于Glide4.9 的 Bitmap
  2. android app 启动会白屏的解决办法
  3. 【Android】TextView常用属性
  4. Android之TableLayout布局
  5. Android中attrs.xml文件的使用详解
  6. Android截屏浅析
  7. Mac下发布Unity3d中Android平台下出现“a
  8. Android之常用类型转换
  9. android 导入工程报错:Unable to resolve
  10. Android解析服务器端发来的xml数据