【问题标题】:How to serve static resources at runtime with Spring Boot?如何使用 Spring Boot 在运行时提供静态资源?
【发布时间】:2021-10-11 22:46:29
【问题描述】:

我有一个 Spring Boot 应用程序,它只有一个 HTML 页面,允许用户上传图像,另一个页面可以查看上传的图像。

这是我的项目结构:

我的 Windows 计算机工作正常,然后我在运行 Spring Boot 应用程序的 Ubuntu 服务器上创建了一个服务,这里的图像上传工作正常,但在另一个 HTML 页面上,图像没有显示并显示如下:

我试图停止服务并重新启动它并且图像正确显示! 所以在运行时看不到新图像,但是当我重新启动它时,图像会出现。

我该如何解决这个问题?

这是我的网络配置:

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                 .addResourceLocations("classpath:/static/");
    }
}

这是我的图像保护程序:

public static void saveFile(String uploadDirImg, String fileName, MultipartFile image) throws IOException {
        Path pathForSavingImg = Paths.get("src", "main", "resources", "static", "db_images");
        Path filePath = pathForSavingImg.resolve(fileName);
        image.transferTo(filePath);
         
    }

【问题讨论】:

    标签: java spring spring-boot tomcat thymeleaf


    【解决方案1】:

    首先让我们解释一下为什么会发生这种行为。您正在使用 Maven 来构建和管理您的项目。为简化起见,每次构建和运行应用程序时,Maven 都会在/target 目录中输出构建结果。您可以在文件系统的 /backend/target 中找到它。

    在应用运行时,应用从/target 文件夹运行,因此类路径是/target/classes/ 而不是/src/main/resources。这非常重要,因为您正在做的是以下内容(我已将 cmets 添加到您的课程中):

    @Configuration
    public class WebConfiguration extends WebMvcConfigurationSupport {
    
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
    
                // here you match any resource path from the URL 
                .addResourceHandler("/**") 
    
                // to search in the classpath:/static/ folder, which in your build
                // is "/target/static" and NOT "/src/main/resources/static"
                .addResourceLocations("classpath:/static/");
        }
    }
    

    我想您现在可以看到问题以及应用程序行为的解释。基本上,您在/src/main/resources/static/db_images 文件夹中上传文件(图像),而应用程序在运行时会在/backend/target/static/db_images 中搜索它们。

    当您重新加载应用程序时,图像会正常工作,因为应用程序会再次重建,重建过程会将db_images 文件夹从/src/main/resources/static/ 复制到/target/static/,并且可以找到之前上传的内容。

    解决问题,一个好的建议和一个已知的做法是使用项目外部的文件系统文件夹并通过绝对路径引用它。上传文件夹通常是您想要从应用源中提取的内容,并且有充分的理由(首先想到的是文件夹的大小)。

    所以如果你有不同的环境,你可以在 application.properties 中添加一些属性来控制上传路径,如下所示:

    # For Windows OS
    upload.folder.path=D:/db_images/
    

    或

    # For Unix-like OS
    upload.folder.path=/srv/db_images/
    

    那么您的 WebConfiguration 应该如下所示:

    @Configuration
    public class WebConfiguration extends WebMvcConfigurationSupport {
    
        @Value("${upload.folder.path}")
        private String uploadFolderPath;
    
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
                .addResourceHandler("/**") 
                .addResourceLocations("file:" + uploadFolderPath);
        }
    }
    

    当然,图像保护程序应该考虑到这一点并进行相应的修改。

    public static void saveFile(String uploadDirImg, String fileName, MultipartFile image) throws IOException {
            Path pathForSavingImg = Paths.get(uploadFolderPath);
            Path filePath = pathForSavingImg.resolve(fileName);
            image.transferTo(filePath);
             
        }
    

    【讨论】:

    • 当应用程序在原始设置下工作时,有一个更简单的解释:OP 可能是从 Maven 或 IDE 启动应用程序。这些工具在启动应用程序时采用快捷方式:例如他们可能只是将src/main/resources 添加到类路径中,而不是将资源复制到target/classes。
    • 太棒了!我在 src/main/resources 下有一个目录,其中包含用户上传的静态内容。所以我不得不再插入一个这样的资源处理程序:registry.addResourceHandler("/blog/**").addResourceLocations("file:src/main/resources/blog/") 然后我可以上传并按时查看图像,不必重置应用程序
    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2016-11-04
    • 2014-09-15
    • 2016-10-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多