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

SpringBoot是一款流行的框架,用于开发Web应用程序。在使用SpringBoot构建Web应用程序时,可能需要实现文件下载的功能。文件下载是指在Web应用程序中提供用户下载文件的功能。这可以是图片、PDF、音频或文档等格式的文件。通常情况下,文件下载需要从服务器端向客户端传输文件SpringBoot为此提供了很多支持。 SpringBoot提供了一种基于Restful的方式进行文件下载。在SpringBoot中,可以使用ResponseEntity<byte[]>类来表示数据,这是一个特殊的ResponseEntity响应类型,它允许向客户端提供二进制数据。要下载文件,需要使用ResponseEntity<byte[]>将文件的字节流封装到响应中。该字节流可以来自于你的本地文件系统、数据库或其他数据源。 为了让用户下载文件,需要根据请求头中的Accept参数来返回正确的文件类型。用Content-Type头来通知浏览器返回的数据类型。可以使用以下代码实现该功能: ``` @GetMapping("/download") public ResponseEntity<byte[]> downloadFile() { // 从文件路径中获取输入流 InputStream input = new FileInputStream(new File("path/to/file")); byte[] bytes = IOUtils.toByteArray(input); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_PDF); // 设置Content-Type headers.setContentDispositionFormData("attachment", "file.pdf"); // 文件名 ResponseEntity<byte[]> response = new ResponseEntity<>(bytes, headers, HttpStatus.OK); return response; } ``` 在这个例子中,使用了Java IO流。首先将从文件路径中获取的输入流读入到字节数组中,然后设置响应的头信息,包括Content-Type和Content-Disposition。最后将字节数组和响应头一起封装到ResponseEntity<byte[]>响应实体中,并返回。 在SpringBoot中,可以轻松实现文件下载的功能。以上代码仅仅是其中之一的实现方式。具体实现方式会依据实际情况有所不同。使用SpringBoot提供的ResponseEntity类提供二进制数据,可以以Restful的方式来下载文件。这样的方式不仅简单,方便,而且支持多种文件类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值