zoukankan      html  css  js  c++  java
  • CharsetUtils.java

    /*
     * Copyright (c) 2013. 
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.example.kjdemo.utils;
    
    import org.apache.http.protocol.HTTP;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by wyouflf on 13-8-30.
     */
    public class CharsetUtils {
    
        private CharsetUtils() {
        }
    
        public static String toCharset(final String str, final String charset, int judgeCharsetLength) {
            try {
                String oldCharset = getEncoding(str, judgeCharsetLength);
                return new String(str.getBytes(oldCharset), charset);
            } catch (Throwable ex) {
                LogUtils.w(ex);
                return str;
            }
        }
    
        public static String getEncoding(final String str, int judgeCharsetLength) {
            String encode = CharsetUtils.DEFAULT_ENCODING_CHARSET;
            for (String charset : SUPPORT_CHARSET) {
                if (isCharset(str, charset, judgeCharsetLength)) {
                    encode = charset;
                    break;
                }
            }
            return encode;
        }
    
        public static boolean isCharset(final String str, final String charset, int judgeCharsetLength) {
            try {
                String temp = str.length() > judgeCharsetLength ? str.substring(0, judgeCharsetLength) : str;
                return temp.equals(new String(temp.getBytes(charset), charset));
            } catch (Throwable e) {
                return false;
            }
        }
    
        public static final String DEFAULT_ENCODING_CHARSET = HTTP.DEFAULT_CONTENT_CHARSET;
    
        public static final List<String> SUPPORT_CHARSET = new ArrayList<String>();
    
        static {
            SUPPORT_CHARSET.add("ISO-8859-1");
    
            SUPPORT_CHARSET.add("GB2312");
            SUPPORT_CHARSET.add("GBK");
            SUPPORT_CHARSET.add("GB18030");
    
            SUPPORT_CHARSET.add("US-ASCII");
            SUPPORT_CHARSET.add("ASCII");
    
            SUPPORT_CHARSET.add("ISO-2022-KR");
    
            SUPPORT_CHARSET.add("ISO-8859-2");
    
            SUPPORT_CHARSET.add("ISO-2022-JP");
            SUPPORT_CHARSET.add("ISO-2022-JP-2");
    
            SUPPORT_CHARSET.add("UTF-8");
        }
    }
  • 相关阅读:
    vue六十五:vuex-getters和vuex-mapGetters
    vue六十四:vuex-mapStore
    vue六十三:vuex的异步处理
    vue六十二:vuex的同步状态管理和调试工具vue-devtools
    vue六十一:使用vuex管理状态示例
    vue六十:电影院售票项目案例之影院页面和使用Better-Scroll实现拖拉滚动
    vue五十九:电影院售票项目案例之详情页面
    vue五十八:电影院售票项目案例之项目吸顶效果
    ingress 413 Request Entity Too Large
    kubeadm搭建K8S集群
  • 原文地址:https://www.cnblogs.com/royi123/p/5653843.html
Copyright © 2011-2022 走看看