【问题标题】:Java Spring Access Static Resources outside applicationJava Spring访问应用程序外部的静态资源
【发布时间】:2019-04-30 02:22:59
【问题描述】:

我想让我的用户上传图像,然后将这些图像存储在服务器上。 由于将这些图像放在应用程序资源/静态文件夹中会在每次新部署时删除它们,因此我想在 Spring 应用程序之外存储和访问这些图像。

我仍然需要在我的 html-Context 中显示图像,所以我要做的是将路径(例如“userimages”)映射到我的应用程序之外的位置(例如“/home/userimages/”所以即当调用 url localhost:8080/userimages/hi.jpg 时,返回 /home/userimages/hi.jpg 中的图像。

我发现似乎能够做到这一点的函数 addResourceHandlers,但我在实现它时遇到了麻烦。对于初学者,我不确定是扩展 WebMvcConfigurationSupport 还是 WebMvcConfigurationAdapter 还是实现 WebMvcConfigurer。似乎没有一个工作。 我还尝试了在该类上方使用不同的@,例如@EnableWebMvc 等-没有任何改变。 一些网站建议将 Class 移到与应用程序相同的包中 - 也没有用。

我认为函数 addResourceHandlers 甚至没有被调用,我不知道如何确保它是。这是目前我的代码:

package global;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class StaticResourceProvider implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //This never gets printed
        System.out.print("Adding resource handler");
        registry
                .addResourceHandler("/user-images/**")
                //for Unix: file:/opt/files
                //TODO: use path variable here
                .addResourceLocations("file:///C:/Users/Juliette/Pictures/");
    }
}

我的应用程序的入口点:

package controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application  extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        //TODO: remove the first array element and read it as the name of this instance
        SpringApplication.run(Application.class, args);
    }

}

谁能告诉我我的错误是什么?目前,当我访问 localhost:8080/user-images/nameOfSomeImage.jpg 时,只返回一个白页,并且服务器日志中没有显示任何消息。

【问题讨论】:

    标签: java spring mapping


    【解决方案1】:

    我现在设法通过将两个类移动到同一个包中并按如下方式修改它们来使其工作:

    package controller;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            //TODO: remove the first array element and read it as the name of this instance
            SpringApplication.run(Application.class, args);
        }
    
    }
    

    以及资源提供者:

    package controller;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class StaticResourceProvider implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
                    .addResourceHandler("/css/**")
                    .addResourceLocations("classpath:/static/css/");
    
            registry
                    .addResourceHandler("/js/**")
                    .addResourceLocations("classpath:/static/js/");
    
            registry
                    .addResourceHandler("/img/**")
                    //for Unix: file:/opt/files
                    //TODO: use path here
                    .addResourceLocations("file:///C:/Users/Juliette/Pictures/");
        }
    }
    

    【讨论】:

    • 非常感谢!看了你的回答,我用【addResourceLocations("file:./public/")】完成了【Jar包-形成当前目录】和【Idea开发模式-项目根目录】的统一。
    猜你喜欢
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多