【问题标题】:How to specify external location for image store in Spring Boot 2.0 web app?如何在 Spring Boot 2.0 Web 应用程序中指定图像存储的外部位置?
【发布时间】:2018-08-16 19:14:07
【问题描述】:

目前我将图像存储在项目目录的 /src/main/resources/static/myimages 中。现在我想将这些移到项目目录之外,例如在 /Users/tom/myimages 中,以便在 HTML 标记中的 img 标签 src="/myimages/subdir/first.jpg" 中从 /Users/tom/myimages/ 加载子目录/first.jpg。如何在 spring boot 2.0 项目中实现这一点?

这将允许我添加新图像,而无需在生产环境中重新编译项目。

【问题讨论】:

  • 也许您可以创建一个从“/Users/tom/myimages”到“/src/main/resources/static/myimages”的符号链接?这些图像在您的项目之外,但您仍然可以在代码中引用它们。

标签: spring image spring-boot


【解决方案1】:

您可以通过PathResourceResolver 实现此目的,这是最简单的解析器,其目的是查找给定公共 URL 模式的资源。事实上,这是默认的解析。

代码:

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry
               .addResourceHandler("/myimages/**")
               .addResourceLocations("/Users/tom/myimages")
               .setCachePeriod(3600)
               .resourceChain(true)
               .addResolver(new PathResourceResolver());
    }
} 

说明:

  • 我们正在将资源链中的PathResourceResolver 注册为其中唯一的ResourceResolver
  • html 代码与PathResourceResolver 一起在/Users/tom/myimages 文件夹中定位/first.jpg 文件

【讨论】:

  • 唯一的事情是,如果我们需要从外部位置提供图像,我们将需要添加“file:/”,如 .addResourceLocations("file:/Users/tom/myimages") 与答案相同@Sh 帕维尔
  • 如果你需要定义一个像/home/.../images/profileImages/user.png这样的目录,那么你需要像addResourceHandler("/images/**")addResourceLocations("file:/home/.../images")这样定义它,然后像这样/images/profileImages/user.png得到它?
【解决方案2】:

您是否应该尝试定义自己的静态资源处理程序?

它将覆盖默认值。

类似这样的:

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:/path/to/my/folder/");
    }
}

更新 这似乎是我的一些旧项目的工作:

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class CustomWebMvcAutoConfig extends
                    WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String myExternalFilePath = "file:///C:/Users/tom/imgs/";

    registry.addResourceHandler("/imgs/**").addResourceLocations(myExternalFilePath);

    super.addResourceHandlers(registry);
  }

}

【讨论】:

  • 你能解释一下你的答案吗?这是否意味着现在是否将从 C:/Users/tom/imgs/ 加载任何包含“imgs”的路径?
  • @ace 是的,是的
【解决方案3】:

Spring 5 - Static Resources

来自文档:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

        public void addResourceHandlers (ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/pages/**").
                      addResourceLocations("classpath:/my-custom-location/","C:/spark/Hadoop/my-custom-location/");


        }
}

或者您可以将图像路径存储在数据库中。在需要时动态加载它,您也可以动态更改该路径。

HTML 代码将是这样的:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<link href='<spring:url value="/resources/css/style.css"/>' rel="stylesheet" />
<script type="text/javascript" src='<spring:url value="/resources/js/app.js"/>'></script>

</head>
<body>
   <h1 id="title" class="color1">Spring MVC- Static Resource Mapping Example</h1>

   <button onclick="changeColor()">Change Color</button>
   <hr />

   <img alt="http://mytechnologythought.blogspot.com" src="<spring:url value="/pages/img01.png"/>" width="200">
</body>
</html>

Here Full Example Reference Blog link 注意:- WebMvcConfigurerAdapter 类型已弃用

【讨论】:

  • 您的回答不清楚。你能把它具体到我的问题吗?我的问题给出了不同的路径。您的答案是否意味着现在是否将从 C:/spark/Hadoop/my-custom-location/ 加载任何包含“页面”的路径? classpath:/my-custom-location 的含义是什么?你能详细说明一下吗?
【解决方案4】:

上面的答案非常好。但正确的方法是根据spring boot docs。您需要将以下属性添加到application.properties文件中

spring.resources.static-locations=file:/home/username/images/

mvc 静态路径模式不是必需的 spring.mvc.static-path-pattern=/resources/**

http://localhost:8080/resources/exampleimage.png

我们提到的资源路径将被添加到默认资源位置列表中

请阅读文档以获取更多信息 https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-static-content

【讨论】:

    猜你喜欢
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2021-12-16
    • 2023-03-21
    • 2018-06-20
    • 2022-12-17
    • 2020-12-17
    相关资源
    最近更新 更多