重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么在SpringBoot项目中实现一个文件下载功能

怎么在Spring Boot 项目中实现一个文件下载功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

10年积累的网站建设、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有萍乡免费网站建设让你可以放心的选择与我们合作。

(一)需求

在您的 springboot 项目中,可能会存在让用户下载文档的需求,比如让用户下载 readme 文档来更好地了解该项目的概况或使用方法。

所以,您需要为用户提供可以下载文件的 API ,将用户希望获取的文件作为下载资源返回给前端。

(二)代码

maven 依赖

请您创建好一个 springboot 项目,一定要引入 web 依赖:


  org.springframework.boot
  spring-boot-starter-web

建议引入 thymeleaf 作为前端模板:


  org.springframework.boot
  spring-boot-starter-thymeleaf

配置 application

在您的 application.yml 中,进行如下属性配置:

file:
 doc-dir: doc/

该路径就是待下载文件存放在服务器上的目录,为相对路径,表示与当前项目(jar包)的相对位置。

怎么在Spring Boot 项目中实现一个文件下载功能

将属性与 pojo 类自动绑定

springboot 中的注解 @ConfigurationProperties 可以将 application 中定义的属性与 pojo 类自动绑定。所以,我们需要定义一个 pojo 类来做 application 中 file.doc-dir=doc/ 的配置绑定:

@ConfigurationProperties(prefix = "file")
@Data
public class FileProperties {
  private String docDir;
}

注解 @ConfigurationProperties(prefix = "file") 在 springboot 应用启动时将 file 为前缀的属性与 pojo 类绑定,也就是将 application.yml 中的 file.doc-dir 与 FileProperties 中的字段 docDir 做了绑定。

激活配置属性

在启动类或其他配置类(@Configuration注解标记)上加入 @EnableConfigurationProperties 即可让 ConfigurationProperties 特性生效。

@SpringBootApplication
@EnableConfigurationProperties({FileProperties.class})
public class AutoTestApplication {
  public static void main(String[] args) {
    SpringApplication.run(AutoTestApplication.class, args);
  }
}

控制层

在控制层我们将以 spring 框架的 ResponseEntity 类作为返回值传给前端,其中泛型为 spring io 包的 Resource 类,这意味着返回内容为 io 流资源;并在返回体头部添加附件,以便于前端页面下载文件。

@RestController
@RequestMapping("file")
public class FileController {
  private static final Logger logger = LoggerFactory.getLogger(FileController.class);
  @Autowired
  private FileService fileService;
  @GetMapping("download/{fileName}")
  public ResponseEntity downloadFile(@PathVariable String fileName,
                         HttpServletRequest request) {
    Resource resource = fileService.loadFileAsResource(fileName);
    String contentType = null;
    try {
      contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
    } catch (IOException e) {
      logger.error("无法获取文件类型", e);
    }
    if (contentType == null) {
      contentType = "application/octet-stream";
    }
    return ResponseEntity.ok()
        .contentType(MediaType.parseMediaType(contentType))
        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
        .body(resource);
  }
}

服务层

服务层的主要工作是把文件作为 IO 资源加载。注意,在 Service 实现类的构造方法中要使用 @Autowired 注入前面定义好的属性绑定类 FileProperties.

@Service
public class FileServiceImpl implements FileService {
  private final Path filePath;
  @Autowired
  public FileServiceImpl(FileProperties fileProperties) {
    filePath = Paths.get(fileProperties.getDocDir()).toAbsolutePath().normalize();
  }
  @Override
  public Resource loadFileAsResource(String fileName) {
    Path path = filePath.resolve(fileName).normalize();
    try {
      UrlResource resource = new UrlResource(path.toUri());
      if (resource.exists()) {
        return resource;
      }
      throw new FileException("file " + fileName + " not found");
    } catch (MalformedURLException e) {
      throw new FileException("file " + fileName + " not found", e);
    }
  }
}

自定义异常

在服务层,我们抛出自定义的文件异常 FileException.

public class FileException extends RuntimeException {
  public FileException(String message) {
    super(message);
  }
  public FileException(String message, Throwable cause) {
    super(message, cause);
  }
}

前端

在前端 html 页面,可以使用 a 标签来下载文件,注意在 a 标签中定义 download 属性来规定这是一个下载文件。

下载使用手册

关于怎么在Spring Boot 项目中实现一个文件下载功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


标题名称:怎么在SpringBoot项目中实现一个文件下载功能
文章路径:http://cqcxhl.com/article/poggss.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP