package com.ruoyi.web.core.config;
import java.io.File;
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.WebMvcConfigurerAdapter;
/**
* 用于设置springboot对静态资源的访问路径,
* 或者在application.properties中配置spring.mvc.static-path-pattern属性,
* 而不是默认的/**
*/
@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter {
@Value("${file.staticAccessPath}")
private String staticAccessPath;
@Value("${file.uploadFolder}")
private String uploadFolder;
@Value("${chat.staticAccessPath}")
private String chatFileAccessPath;
@Value("${chat.chatFolder}")
private String chatFolder;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
File f = new File(uploadFolder);
File chatFile = new File(chatFolder);
//如果文件夹不存在则创建
if (!f.exists() && !f.isDirectory()) {
f.mkdir();
}
if (!chatFile.exists() && !chatFile.isDirectory()) {
chatFile.mkdir();
}
registry.addResourceHandler(staticAccessPath)
.addResourceLocations("file:" + uploadFolder);
registry.addResourceHandler(chatFileAccessPath)
.addResourceLocations("file:" + chatFolder);
super.addResourceHandlers(registry);
}
}