# SpringBoot+thymeleaf 实现文件下载 **Repository Path**: xiaorenhua/springBoot_thymeleaf_download_file ## Basic Information - **Project Name**: SpringBoot+thymeleaf 实现文件下载 - **Description**: SpringBoot+thymeleaf 实现文件下载 - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2022-07-06 - **Last Updated**: 2022-07-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SpringBoot文件下载 ## 1.项目结构 ![image-20210812092246120](SpringBoot文件下载.assets/image-20210812092246120.png) ## 2.FileController.java ```java package xin.baizhiedu.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.net.URLEncoder; @Controller @RequestMapping("/file") public class FileController { private static final Logger log = LoggerFactory.getLogger(FileController.class); @Value("${file.download.dir}") private String realPath; /** * 文件下载 * @param fileName */ @RequestMapping("/download") public void downloadFile(String fileName, HttpServletResponse response) throws Exception { log.debug("当前下载的文件名是:{}", fileName); log.debug("当前下载的文件的目录是:{}", realPath); // 1.去指定目录读取文件 File file = new File(realPath, fileName); // 2.将文件读取为文件输入流 FileInputStream is = new FileInputStream(file); // 2.1 获取响应流之前 一定要设置以附件形式下载 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); // 3.获取响应输出流 ServletOutputStream os = response.getOutputStream(); // 4.输入流复制给输出流 /*int len = 0; byte[] buff = new byte[1024]; while (true) { len = is.read(buff); if (len==-1) break; os.write(buff, 0, len); }*/ // 5.释放资源 /*os.close(); is.close();*/ FileCopyUtils.copy(is,os); } } ``` ## 3.index.html ```html index

Hello index !

测试文件下载


HELP.md
readme.txt
项目介绍.md
``` ## 4.application.yml ```yaml server: port: 8080 # servlet: # context-path: /springboot_day06 logging: level: root: info xin.baizhiedu.controller: debug file: download: dir: D:\IntelliJ IDEA 202101Projects\SpringBoot-Baizhiedu\springboot_day06-download\download # 指定下载目录测试环境 # /home/download # 指定上线环境的目录 ``` ## 5.pom.xml ```xml 4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.0 xin.baizhiedu springboot_day06-download 0.0.1-SNAPSHOT springboot_day06-download Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin ``` ## 6.测试 ![image-20210812093916830](SpringBoot文件下载.assets/image-20210812093916830.png) ![image-20210812092613873](SpringBoot文件下载.assets/image-20210812092613873.png) ![image-20210812092733934](SpringBoot文件下载.assets/image-20210812092733934.png)