简体   繁体   中英

Spring Boot:How to display image from system directory in spring boot application?

I am working with spring boot I want to display image from my system directory in html page but i cannot do that. Any can help me please.

This is my html code

<!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>

I tried the following code also but it is not working

<!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>

You need to add configuration like this:

@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);
}
}

add your image path in addResourceLocations .

**If you are Windows user then add path like this: "file:D:\\\\imagepath\\")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM