zoukankan      html  css  js  c++  java
  • Java判断bytes编码

    <dependency>
                <groupId>com.googlecode.juniversalchardet</groupId>
                <artifactId>juniversalchardet</artifactId>
                <version>1.0.3</version>
    </dependency>
        //判断byte编码类型
        public static String getEncoding(byte[] bytes) {
            String DEFAULT_ENCODING = "UTF-8";
            UniversalDetector detector =new UniversalDetector(null);
            detector.handleData(bytes, 0, bytes.length);
            detector.dataEnd();
            String encoding = detector.getDetectedCharset();
            detector.reset();
            if (encoding == null) {
                encoding = DEFAULT_ENCODING;
            }
            return encoding;
        }
         //此处为http获取到的bytes
            byte[] bytes = new httppost().getdata(url,null);
            String encoding = Tools.getEncoding(bytes);//编码判断
            try {
                if(encoding.indexOf("GB")>=0){//由于GBK编码有多种,此处这样判断即可
                    result = new String(bytes,"gbk");
                }
                if("UTF-8".equals(encoding)) {
                    result = new String(bytes, "utf-8");
                }
            } catch (UnsupportedEncodingException e) {
                System.out.println("抓取新ERP柜子数量编码失败");
                e.printStackTrace();
            }
  • 相关阅读:
    noip2008提高组题解
    noip2007提高组题解
    noip2006提高组题解
    一些题目(3)
    一些题目(2)
    洗牌问题
    一些题目
    同余模算术中的乘法逆元
    省常中模拟 Test2 Day2
    用矩阵乘法优化递推
  • 原文地址:https://www.cnblogs.com/i-tao/p/12970639.html
Copyright © 2011-2022 走看看