gpt4 book ai didi

java - Spring FileSystemResource NoSuchFileException

转载 作者:行者123 更新时间:2023-12-02 13:26:35 25 4
gpt4 key购买 nike

我正在使用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:
不幸的是我不知道如何捕捉错误。
如果需要,您可以用Java回答,我可以理解两种语言。

最佳答案

你也可以利用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com