栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring WebFlux:从控制器提供文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring WebFlux:从控制器提供文件

首先,使用Spring MVC实现该目标的方法应如下所示:

@RestController@RequestMapping("/files")public class FileController {    @GetMapping("/{fileName}")    public Resource getFile(@PathVariable String fileName) {        Resource resource = new FileSystemResource(fileName);     return resource;    }}

另外,如果您 在没有其他逻辑的情况下 托管这些资源,则可以使用Spring
MVC的静态资源支持。使用Spring
Boot,

spring.resources.static-locations
可以帮助您自定义位置。

现在,使用Spring WebFlux,您还可以配置相同的

spring.resources.static-locations
配置属性来提供静态资源。

它的WebFlux版本看起来完全一样。如果您需要执行涉及某些I / O的逻辑,则可以返回a

Mono<Resource>
而不是
Resource
直接返回a ,如下所示:

@RestController@RequestMapping("/files")public class FileController {    @GetMapping("/{fileName}")    public Mono<Resource> getFile(@PathVariable String fileName) {        return fileRepository.findByName(fileName)      .map(name -> new FileSystemResource(name));    }}

请注意,使用WebFlux,如果返回

Resource
的实际上是磁盘上的文件,我们将利用零复制机制,这将使事情更高效。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660343.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号