栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

地址省市区提取、地址智能识别

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

地址省市区提取、地址智能识别

先看效果,代码在后面

一、效果

1、四级解析 省-市-区-详细地址
解析前:四川凉山彝族自治州冕宁县泸沽镇11号

解析后:{"msg":"success","code":0,"data":{"area":"冕宁县","address":"四川凉山州冕宁县泸沽镇11号","code":"0","province":"四川","city":"凉山州","detail":"泸沽镇11号"}}

2、三级解析 省-市-区
解析前:四川凉山彝族自治州冕宁县 (有无详细地址都无所谓)

解析后:{"msg":"success","code":0,"data":{"area":"冕宁县","province":"四川","city":"凉山州"}}

3、二级解析 省-市
解析前:四川凉山彝族自治州 (有无区都无所谓)

解析后:{"msg":"success","code":0,"data":{"province":"四川","city":"凉山州"}}

4、缺少区 根据街道自动识别完整地址
该解析方式无法做到非常精确,若该市中存在多个同名街道,则无法识别
解析前:浙江省杭州市古荡街道11号

解析后:{"msg":"success","code":0,"data":{"area":"西湖区","address":"浙江杭州市西湖区古荡街道11号","code":"0","province":"浙江","city":"杭州市","remark":"区无法识别后,识别出街道后后重新识别","detail":"古荡街道11号"}}

5、缺少省份 自动识别出省份
解析前:凉山彝族自治州冕宁县泸沽镇11号

解析后:{"msg":"success","code":0,"data":{"area":"冕宁县","address":"四川凉山州冕宁县泸沽镇11号","code":"0","province":"四川","city":"凉山州","detail":"泸沽镇11号"}}

6、缺少市 自动识别出市
解析前:四川冕宁县泸沽镇11号

解析后:{"msg":"success","code":0,"data":{"area":"冕宁县","address":"四川凉山州冕宁县泸沽镇11号","code":"0","province":"四川","city":"凉山州","detail":"泸沽镇11号"}}
二、生成代码
public static void main(String[] args) throws IOException {
        // 地址
        String addr = "四川冕宁县泸沽镇11号";
        // 级数,如果要将省市区详细地址都解析,那就传递null,如果只需要省市区就传3,只需要省市就传2,只需要省就传1
        String series = null;
        String aesKey = "023382397f9848458967d2b5b9462205";
        Long userId = 18293l;
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            JSONObject jsonObject = new JSONObject();
            JSONObject head = new JSONObject();
            head.put("userId", userId);
            head.put("time", AesUtils.encrypt(time.format(new Date()), aesKey));
            jsonObject.put("head", head);
            jsonObject.put("addr", addr);
            jsonObject.put("series", series);
            Connection.Response execute = Jsoup.connect("http://www.cyho.top/areaAnalysis/analysis")
                    .requestBody(jsonObject.toJSONString())
                    .ignoreContentType(true).method(Connection.Method.POST)
                    .header("content-type", "application/json;charset=UTF-8").execute();
            System.out.println(execute.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
三、工具类

1、其中JSON类是com.alibaba的,可以随便用其他代替,都一样的

        
            com.alibaba
            fastjson
            1.2.78
        

2、发送请求是使用的Jsoup,也可以用其他方式代替

        
            org.jsoup
            jsoup
            1.12.1
        

3、aes加密工具类

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

public class AesUtils {
    // 加密算法
    private static final String ENCRY_ALGORITHM = "AES";
    // 设置加密字符集
    private static final String CHARACTER = "UTF-8";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    
    public static String encrypt(String content, String password) throws Exception {
        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
        byte[] byteContent = content.getBytes(CHARACTER);
        cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
        byte[] result = cipher.doFinal(byteContent);
        return parseByte2HexStr(result);
    }

    
    public static String decrypt(String content, String password) throws Exception {
        try {
            //实例化
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            //使用密钥初始化,设置为解密模式
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
            //执行操作
            byte[] result = cipher.doFinal(parseHexStr2Byte(content));
            return new String(result, CHARACTER);
        } catch (Exception e){
            throw new Exception("解密失败");
        }

    }

    
    private static SecretKeySpec getSecretKey(final String password) throws Exception {
        //返回生成指定算法密钥生成器的 KeyGenerator 对象
        KeyGenerator kg = null;
        kg = KeyGenerator.getInstance(ENCRY_ALGORITHM);
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(password.getBytes());
        kg.init(128, secureRandom);
        SecretKey secretKey = kg.generateKey();
        return new SecretKeySpec(secretKey.getEncoded(), ENCRY_ALGORITHM);
    }

    
    public static String parseByte2HexStr(byte buf[]) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1025036.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号