cm-cloud/cloud-common/src/main/java/com/cm/common/config/WebConfig.java

51 lines
1.8 KiB
Java

package com.cm.common.config;
import com.cm.common.config.properties.FileProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.servlet.MultipartConfigElement;
import java.io.File;
/**
* @ClassName: WebConfig
* @Description: Web配置
* @Author: WangGeng
* @Date: 2019/3/13 9:58 AM
* @Version: 1.0
**/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private FileProperties fileProperties;
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/files/**").addResourceLocations("file:" + fileProperties.getUploadPath());
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/static/assets/").setCachePeriod(7 * 24 * 3600);
}
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
String location = System.getProperty("user.dir") + "/data/tmp";
File file = new File(location);
if(!file.exists()){
file.mkdirs();
}
multipartConfigFactory.setLocation(location);
return multipartConfigFactory.createMultipartConfig();
}
}