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();
            }
  • 相关阅读:
    手动编译安装nginx
    centoos 安装hadoop集群
    block中如何避免循环引用
    正则表达式
    iOS开发ARC内存管理
    block的内部实现
    Block存储区域
    block的语法
    Collection(数组、字典、集合)
    block捕获自动变量和对象
  • 原文地址:https://www.cnblogs.com/i-tao/p/12970639.html
Copyright © 2011-2022 走看看