lxw
2022-05-21 9058c7178cd9a7aece34b278b4c7bec488f95cc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.whyc.config;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whyc.pojo.YamlProperties;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
 
/**
 * @Description : static resources Config
 * @date 2020/09/15
 **/
@Configuration
@EnableWebMvc
public class StaticResourceConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
 
        //这个是可行的,解析的时候path为*.html,校验路径admin下是否存在
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
        /*registry.addResourceHandler("/map/*").addResourceLocations("classpath:/META-INF/resources/map/");
        registry.addResourceHandler("/map/images/*").addResourceLocations("classpath:/META-INF/resources/map/images/");
        registry.addResourceHandler("/map/library/*").addResourceLocations("classpath:/META-INF/resources/map/library/");
 
        //registry.addResourceHandler("/login.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addR  esourceHandler("*.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/service-worker.js").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/license/**").addResourceLocations("classpath:/META-INF/resources/config/");
        registry.addResourceHandler("/js/*").addResourceLocations("classpath:/META-INF/resources/js/");
        registry.addResourceHandler("/img/*").addResourceLocations("classpath:/META-INF/resources/img/");
        registry.addResourceHandler("/theme/*").addResourceLocations("classpath:/META-INF/resources/theme/");
        registry.addResourceHandler("/theme/img/science-blue/*").addResourceLocations("classpath:/META-INF/resources/theme/img/science-blue/");
        registry.addResourceHandler("/fonts/*").addResourceLocations("classpath:/META-INF/resources/fonts/");*/
        //项目jar同级目录下,图片能通过http方式访问到,很重要!
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
 
 
        //开发路径
        //String dirPath = jarFile.getParentFile().toString()+File.separator+"fg_photo_3DStation"+File.separator;
        //打包路径
        String dirPath = jarFile.toString()+File.separator+"fg_photo_3DStation"+File.separator;
        registry.addResourceHandler("/fg_photo_3DStation/**").addResourceLocations("file:/"+dirPath);
 
        //开发路径
        String dirPathA059 = jarFile.getParentFile().toString()+File.separator+"A059"+File.separator+"images"+File.separator;
        //打包路径
        //String dirPathA059 = jarFile.toString()+File.separator+"A059"+File.separator+"images"+File.separator;
        registry.addResourceHandler("/A059/images/**").addResourceLocations("file:"+dirPathA059);
 
        /**统一图片上传路径格式*/
        String baseDirPath;
        String baseDirPath2;
 
        if(YamlProperties.runModel == 1) {
            //开发路径
            //baseDirPath = jarFile.getParentFile().toString()+File.separator;
            baseDirPath2 = jarFile.getParentFile().toString()+File.separator+"fg_photo"+File.separator;
        }else {
            //打包路径
            //baseDirPath = jarFile.toString()+File.separator;
            baseDirPath2 = jarFile.toString()+File.separator+"fg_photo"+File.separator;
        }
        //registry.addResourceHandler("/**").addResourceLocations("file:/"+baseDirPath);
        registry.addResourceHandler("/fg_photo/**").addResourceLocations("file:/"+baseDirPath2);
 
        super.addResourceHandlers(registry);
 
    }
 
    @Bean
    public ObjectMapper jacksonObjectMapperCustomization() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
        format.setTimeZone(timeZone);
 
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
                .timeZone(timeZone)
                .dateFormat(format);
 
        return builder.build();
    }
 
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(c -> c instanceof MappingJackson2HttpMessageConverter);
        converters.add(new MappingJackson2HttpMessageConverter(jacksonObjectMapperCustomization()));
    }
 
}