springboot解决访问静态文件报错和springboot解析ip所在地(使用)

1、静态文件访问

方式一 (不推荐)

将静态文件放在/resources/static目录下,springboot启动会自动去扫描,将/static/当作存在静态文件目录(会出现莫名奇妙的问题)
在这里插入图片描述

方式二(不推荐,和方式一差不多)

在application.properties或者application.yml文件里面配置静态文件的路径)
在这里插入图片描述

方式三(推荐)

使用配置去定义静态文件的路径

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}

在这里插入图片描述

2、解析ip所在地

解析用户ip地址

public static String getIpAddr(HttpServletRequest request){
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("X-Forwarded-For");
            if (ipAddress != null && ipAddress.length() != 0 && !"unknown".equalsIgnoreCase(ipAddress)) {
                // 多次反向代理后会有多个ip值,第一个ip才是真实ip
                if (ipAddress.indexOf(",") != -1) {
                    ipAddress = ipAddress.split(",")[0];
                }
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
            }
        }catch (Exception e) {
            log.error("IPUtils ERROR ",e);
        }
        return ipAddress;
    }

获取ip属地

public static String getCityInfo(String ip) throws Exception {
       //获得文件流时,因为读取的文件是在打好jar文件里面,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流
       ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
       Resource[] resources = resolver.getResources("ip2region.db");
       Resource resource = resources[0];
       InputStream is = resource.getInputStream();
       File target = new File("ip2region.db");
       FileUtils.copyInputStreamToFile(is, target);
       is.close();
       if (StringUtils.isEmpty(String.valueOf(target))) {
           log.error("Error: Invalid ip2region.db file");
           return null;
       }
       DbConfig config = new DbConfig();
       DbSearcher searcher = new DbSearcher(config, String.valueOf(target));
       //查询算法
       //B-tree, B树搜索(更快)
       int algorithm = DbSearcher.BTREE_ALGORITHM;
       try {
           //define the method
           Method method;
           method = searcher.getClass().getMethod("btreeSearch", String.class);
           DataBlock dataBlock;
           if (!Util.isIpAddress(ip)) {
               log.error("Error: Invalid ip address");
           }
           dataBlock = (DataBlock) method.invoke(searcher, ip);
           String ipInfo = dataBlock.getRegion();
           if (!StringUtils.isEmpty(ipInfo)) {
               ipInfo = ipInfo.replace("|0", "");
               ipInfo = ipInfo.replace("0|", "");
           }
           return ipInfo;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return null;
   }

封装方法

 public static String getIpPossession(String ip) throws Exception {
        String cityInfo = IpUtils.getCityInfo(ip);
        if (!StringUtils.isEmpty(cityInfo)) {
            cityInfo = cityInfo.replace("|", " ");
            String[] cityList = cityInfo.split(" ");
            if (cityList.length > 0) {
                // 国内的显示到具体的省
                if ("中国".equals(cityList[0])) {
                    if (cityList.length > 1) {
                        return cityList[1];
                    }
                }
                // 国外显示到国家
                return cityList[0];
            }
        }
        return "未知";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值