springboot by url to access the local picture

1. Introducing a jar package

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. Create a configuration class

package com.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @BelongsProject: demo
 * @Author: DanBrown
 * @CreateTime: 2020-03-28 14:33
 * @description: TODO
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${upload.path}")
    privateUploadPath String; 

    @Override 
    public  void addResourceHandlers (ResourceHandlerRegistry Registry) {
         //   / Home / File / ** URL for the front-end access path behind file: xxxx Local Disk Mapping 
        registry.addResourceHandler ( "/ home / file / **"). addResourceLocations ( "File: C:" + uploadPath); 
    } 

}

3. Access

http://localhost:8080/home/file/820123.png

Guess you like

Origin www.cnblogs.com/DanBrown/p/12588768.html