当前位置: 首页 > news >正文

Springboot 文件下载

文件下载


添加依赖:通常,文件下载功能不需要额外的依赖,但确保你的pom.xml或build.gradle文件中包含Spring Web依赖。

<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>
package com.sh.system.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("/files")
public class FileController {

    // 从服务器文件系统下载文件
    @GetMapping("/download/server")
    public ResponseEntity<byte[]> downloadFromServer(HttpServletResponse response) throws IOException {
        Path filePath = Paths.get("D:\\测试下载.docx");
        byte[] data = Files.readAllBytes(filePath);
        //
        String originalFileName = filePath.getFileName().toString();
        String fileName = new String(originalFileName.getBytes("UTF-8"), "ISO-8859-1");
        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(data.length)
                .body(data);
    }



    // 从类路径下下载文件
    @GetMapping("/download/classpath")
    public ResponseEntity<Resource> downloadFromClasspath(HttpServletResponse response) throws IOException {
        Resource resource = new ClassPathResource("static/deepSeek说明.docx");
        String originalFileName = resource.getFilename();
        String fileName = new String(originalFileName.getBytes("UTF-8"), "ISO-8859-1");
        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }
}


放置测试文件:
如果你选择从类路径下载文件,将文件(例如example.txt)放在src/main/resources/static/目录下。
如果你选择从服务器文件系统下载文件,确保文件路径正确,并且Spring Boot应用有权限访问该文件。
运行应用:启动Spring Boot应用,并访问以下URL测试文件下载功能:
类路径文件下载:http://localhost:8080/files/download/classpath
服务器文件系统文件下载:http://localhost:8080/files/download/server

相关文章:

  • 【JavaEE进阶】Spring Boot配置文件
  • 本地Oracle数据库复制数据到Apache Hive的Linux服务器集群的分步流程
  • angular登录页
  • ARCGIS国土超级工具集1.4更新说明
  • Java Map实现类面试题
  • 位于陕西省的高校查收查引单位
  • 04.Python函数和模块
  • DeepSeek+Kimi 一键生成100种PPT
  • 商业化运作的“日记”
  • 突破多模态与跨领域瓶颈!清华华为联合推出革命性推荐系统专利,重塑AI时代用户体验
  • AF3 创新点总结
  • 【FAQ】HarmonyOS SDK 闭源开放能力 —Ads Kit(2)
  • 【算法】位运算
  • OpenCV计算摄影学Computational Photography
  • 【IEEE出版,往届会后3个月EI检索 | 西华大学主办 | 中英文期刊、SCI期刊推荐】第四届能源、电力与电气国际学术会议(ICEPET 2025)
  • DeepSeek+Kimi生成高质量PPT
  • 智慧家居中的视频监控系统-------2、MJPG-streamer方案的实现
  • 迅为RK3568开发板篇Openharmony配置HDF控制UART-实操-HDF驱动配置UART-配置 rk3568_uart_config.hcs
  • 即将发布书籍 - Yocto项目实战教程:高效定制嵌入式Linux系统
  • 为什么要将PDF转换为CSV?CSV是Excel吗?
  • 当“小铁人”遇上青浦,看00后如何玩转长三角铁三
  • 以军证实空袭也门多个港口
  • 小雨伞保险经纪母公司手回集团通过港交所聆讯
  • 师爷、文士、畸人:会稽范啸风及其著述
  • 圆桌丨新能源车超充技术元年,专家呼吁重视电网承载能力可能面临的结构性挑战
  • 商务部新闻发言人就出口管制管控名单答记者问