java - SpringBoot : java. io.FileNotFoundException: 文件系统中无法解析文件[]以检查其内容长度

标签 java spring spring-boot

我试图从 springboot Controller 返回图像作为资源对象,但收到错误 java.io.FileNotFoundException: file [/home/user/eclipse-workspace/MedVibes-1/Ironman.png] 无法在文件系统中解析以检查其内容长度

Controller

@RestController
    public class MedVibesController {
        @Autowired
        ResourceLoader resourceLoader;
        @RequestMapping(value = "/file", method = RequestMethod.GET)
        @ResponseBody
        Resource resource = new FileSystemResource("ironman.png");
    return resource;
    }

我在 MedVibes-1 目录及其 static 文件夹中都有一个 Ironman.png 文件 enter image description here

我仍然遇到 FileNOtFoundException 吗?我的代码有什么问题?如何修复此错误?

最佳答案

FileSystemResource 在文件系统中查找文件。如果你想使用它,你必须指定完整路径。

您可以使用ClassPathResource

Resource resource = new ClassPathResource("static/ironman.png");

默认情况下,它将查看资源文件夹。

另一种方式

@Autowired
ResourceLoader resourceLoader;

Resource resource = resourceLoader.getResource("classpath:ironman.png");

关于java - SpringBoot : java. io.FileNotFoundException: 文件系统中无法解析文件[]以检查其内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60855729/

相关文章:

java - 多个 JSlider 相互 react 始终等于 100%

java - Set<Class<PupilVO>> 和 Set<PupilVO> 的区别

java - 根据重力移动 ImageView?

java - Checkmarx SQL 注入(inject)高严重性问题

java - 在 Spring Boot 中使用 Java 配置在 bean 定义之外为 bean 别名

java - 构造日期 - 一种有效的方法

java - SpringBoot Bean类型找不到

java - 无法反序列化 HTTP 调用程序远程服务的结果 [...];嵌套异常是 java.lang.ClassNotFoundException :

java - Spring boot 中的请求是如何生成 JSON 值的?

java - Spring Config 服务器在运行时添加属性