【问题标题】:Spring Boot:How to display image from system directory in spring boot application?Spring Boot:如何在 Spring Boot 应用程序中显示系统目录中的图像?
【发布时间】:2018-07-29 07:48:54
【问题描述】:

我正在使用 Spring Boot 我想在 html 页面中显示我的系统目录中的图像,但我不能这样做。 任何人都可以帮助我。

这是我的html代码

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<img alt="no image" src="D:\images\fav-icon">
</body>

我也尝试了以下代码,但它不起作用

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<img alt="no data" th:src="@{D:\images\1r-product-6.jpg}">
</body>
</html>

【问题讨论】:

    标签: html spring-boot thymeleaf


    【解决方案1】:

    你需要像这样添加配置:

    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
            .addResourceHandler("/images/**")
            .addResourceLocations("file:resources/","file:uploads/","file:/your-image-path")
            .setCachePeriod(0);
    }
    }
    

    在addResourceLocations中添加你的图片路径。

    **如果您是 Windows 用户,则添加如下路径:"file:D:\\imagepath\")

    【讨论】:

    • 感谢您的回答。但是我应该在 html 页面中图像标签的 src 属性中给出什么值。
    • 你给路径/images:@{/images/thymeleaf.png}
    • thymeleaf.png 是图像的名称及其扩展名?
    • 我只是举个例子
    • 你使用你的图片名称
    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多