When loading pictures using html, I found that the local picture cannot be displayed on the page, but it can be displayed by directly referring to resources on the network. After referring to the experience of many predecessors, we came to the conclusion:
The main problem that local images cannot be displayed is that the URLs of the image in the local URL and the image loaded on the server are different. That is, the problem of paths.
The solution is actually very simple. Just write a configuration file, that is, the converter of the image location. The principle is to virtually match a folder on the server to the location of the local image.
When calling the local image, it is equivalent to calling the image on the server.
The key code is as follows:
@Configurationpublic class MyWebAppConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /** * @Description: Configure the path of the file and create a virtual path /Path/** , that is, you can directly reference the image in <img src="/Path/picName.jpg" />* This is the physical path of the image "file:/+ the address of the local image" * @Date: Create in 14:08 2017/12/20 */ registry.addResourceHandler("/Path/**").addResourceLocations("file:/E:/WebPackage/IdeaProjects/shiroLearn/src/main/resources/static/"); super.addResourceHandlers(registry); }}Let's take a look at the problem of not being able to read local images in Spring boot html
You need to set the static resource path of spring boot, specifically add it in application.properties:
spring.resources.static-locations='静态资源路径'
If the following configuration specifies the classpath as the static resource path:
spring.resources.static-locations=classpath:/
If my image qiaoba.jpeg is placed under src/main/resoruces/images , then I can reference the image in html like this:
<img src="images/qiaoba.jpeg">
Summarize
The above is the problem and solution to spring boot local pictures cannot be loaded (image path) that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!