【问题标题】:Spring get files and images from external folder on disk, outside webapps?Spring从磁盘上的外部文件夹中获取文件和图像,在webapps之外?
【发布时间】:2015-12-02 20:19:31
【问题描述】:

我有一个 webproject,它在 src/main/webapp 文件夹中有图像。我想将图像放在磁盘上的不同文件夹中。但我不知道如何管理访问这些图像的请求。 我应该创建某种httpServlet,如下所示:http://balusc.omnifaces.org/2007/07/fileservlet.html

或者当我使用带有java配置的Spring MVC时,有更合适和更简单的方法。

期待您的建议。

【问题讨论】:

    标签: java spring resources external


    【解决方案1】:

    使用弹簧mvc support for static resources,它的location属性是一个弹簧Resource,所以你可以使用file:前缀

    <resources mapping="/resources/**" location="file:/my/external/directory/" />
    

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**")
                    .addResourceLocations("file:/my/external/directory/");
        }
    }
    

    @查看Spring Reference Chapter 6.4 The ResourceLoader 获取列出所有资源前缀的表格

    【讨论】:

      【解决方案2】:

      在我的例子中,我将 js 和 css 资源放在 webapp 中,并将图像放在 e://images/ 中。 对于这种情况,我使用两个 mvc:resources 映射

      <mvc:resources mapping="/resources/**" location="/resources/"/>
      <mvc:resources mapping="/images/**" location="file:e://images/"/>
      

      并使用 ....

      定位 e: > images > abc.jpg 的图像
      <img src="../images/abc.jpg"/>
      

      你也可以试试&lt;img src="/images/abc.jpg"/&gt; or &lt;img src="images/abc.jpg"&gt;(如果不行)

      webapp > resources > js > xyz.js 下链接的 css/js 如下所示。

      <script type='text/javascript' src='../resources/js/xyz.js'></script>
      

      【讨论】:

      • 您好,以上方法我都试过了。不适用于任何语法组合。收到 404 错误。这就是我在 dispatcher-servlet.xml 文件中添加的内容
      • @techhunter 尝试使用我的位置参考中的确切斜线。我看到您在驱动器名称 (/D) 之前使用了一个额外的斜杠。请删除驱动器名称中多余的斜杠和小写字母。
      • 非常感谢您的回复。将图像的扩展名从“.jpg”更改为“.JPG”后,上述方法对我有用。赞成答案。
      • @techhunter 感谢您的投票。顺便说一句,您可以在 xml 中添加更多资源映射和位置。
      • 您好如何从浏览器访问此图像。
      【解决方案3】:

      您可以在您的主应用程序或主类中implement WebMvcConfigurer

      @SpringBootApplication
      @ComponentScan("com.your.package.controller")
      @Configuration
      public class ServicesApplication implements WebMvcConfigurer {   // ServicesApplication is my main class
      
          String your_drive_location = "file:///D:/upload_dir/upload/"; // my file path  
      
          @Override
          public void addResourceHandlers(ResourceHandlerRegistry registry) {
              registry.addResourceHandler("/images/**").addResourceLocations(your_drive_location );
          }
      
          public static void main(String[] args) {
              SpringApplication.run(ServicesApplication.class, args);
          }
      
      }
      

      我正在使用 JSTL 进行展示:

      <c:forEach items="${ListOfBrand}" var="brand">  // ListOfBrand is a Map
          <img src=" <c:out value="/images/"/>${brand.brand_image}" />  // Showing all image from brand table
      </c:forEach>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-21
        • 2019-08-11
        • 2015-07-23
        相关资源
        最近更新 更多