2.x版本结局方案如下
创建一个配置类
package com.deve.config;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* Author:zyk
* createTime:2022/1/24 14:52
* desc:
*/
@Configuration
public class UploadFilePathConfig implements WebMvcConfigurer {
@Value("${fileData.staticAccessPath}")
private String staticAccessPath;
@Value("${fileData.uploadRealPath}")
private String uploadRealPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+uploadRealPath);
}
}
写配置文件
fileData:
staticAccessPath: /uploadFiles/**
uploadRealPath: E:/SpringBootUploadFiles/
将本地文件包装为一个虚拟路径
当访问http:127.0.0.1:8081/uploadFiles/图片.jpg时候
就相当于访问 E:/SpringBootUploadFiles/图片.jpg