- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用FileSystemResource和Spring Webflux从硬盘驱动器提供文件。
@GetMapping("/news/file")
fun getImage(@RequestParam name: String): FileSystemResource {
return FileSystemResource(propsStorage.path + "/" + name)
}
当用户请求未知文件时,应将其捕获并返回404错误。
java.nio.file.NoSuchFileException: C:\Users\SomeUser\Work\posts\32.jpg
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
不幸的是我不知道如何捕捉错误。
最佳答案
你也可以利用spring FilesystemResource
api存在的方法来避免FileNotFoundException
fs =FileSystemResource(propsStorage.path + "/" + name)
if(fs.exists())
return fs
else
return new ResponseEntity<>("File Not Found", HttpStatus.NOT_FOUND);
另外我还看到您使用'/'作为分隔符,请注意,在Windows计算机上,文件分隔符为”。因此,为了正确遵守,请使用
Paths.separator
关于java - Spring FileSystemResource NoSuchFileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63884242/
我正在使用FileSystemResource和Spring Webflux从硬盘驱动器提供文件。 @GetMapping("/news/file") fun getImage(@RequestPar
我有以下返回文件的 spring mvc 方法: @RequestMapping(value = "/files/{fileName}", method = RequestMethod.GET) pu
我想下载一个文件,同时保留该文件的文件名。 我有: @RequestMapping(value = "/downloadFile", method = RequestMethod.GET,
我尝试从 URL(我的 DropBox 帐户)创建如下所示的 fil,然后将其作为附件添加到 MimeMessageHelper,但我收到 FileNotFoundException。我可能做错了什么
我正在尝试检索类路径中的文件列表,但 Spring 返回的是 FileSystemResources 的 Resource[] 而不是 ClassPathResources。代码如下所示: PathM
是否可以将 MultipartFile 转换为 FileSystemResource? 我有 @RequestParam("file") MultipartFile file 然后 prmPowerP
我需要将我们所有的属性文件存储在一个目录中。此目录的位置应存储在系统环境变量中。在我的应用程序上下文中,我需要访问这个环境变量来创建 FileSystemResource bean。这是我通常拥有的示
我有以下用于下载文件的代码:- @Controller public class FileController { @RequestMapping(value = "/files/{file_
问题: 我创建了一个程序,它将使用 FilesystemResource 从指定路径读取 applicationContext.xml 文件并调用 B.Method。我无法理解为什么我会收到这样的错误
我在 Spring boot 中打开证书存储时遇到问题。我的应用程序是一个 war 文件,并且证书存储位于外部路径上。 我得到的错误是:嵌套异常为 java.lang.IllegalStateExce
我正在使用 Java Mail 发送带有附件的电子邮件。附件不是本地存储在应用程序下,而是从 URL 动态提取。 List fileSystemResources = new ArrayLis
我有一种下载 zip 文件的方法,目前我使用 FileSystemResource 来执行此操作。问题在于扩展 Controller ,我检查文件是否确实是 zip 文件,如果不是,则抛出异常。此异常
我正在尝试使用 Spring MVC 应用程序的基于 Java 的配置从 WEB-INF 读取属性文件。当我将属性目录放在 src 目录中并使用 class: (ClassPathResource)
我是一名优秀的程序员,十分优秀!