欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

上传文件相对路径设置

程序员文章站 2022-05-09 22:58:39
...

yml中

xxx:
  uploadPath: D:/files/

配置类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="xxx")
public class XxxConfig {
	//上传路径
	private String uploadPath;

	public String getUploadPath() {
		return uploadPath;
	}

	public void setUploadPath(String uploadPath) {
		this.uploadPath = uploadPath;
	}
}

静态资源加载

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Component
class WebConfigurer extends WebMvcConfigurationSupport {
	@Autowired
	XxxConfig xxxConfig;
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//静态资源加载相对路径
		registry.addResourceHandler("/files/**").addResourceLocations("file:///"+schoolConfig.getUploadPath());
	}

}