/*
* 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");
}
}

CharsetUtils.java的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 【java】org.apache.commons.lang3功能示例

    org.apache.commons.lang3功能示例 package com.simple.test; import java.util.Date; import java.util.Iterat ...

  3. java项目中可能会使用到的jar包解释

    一.Struts2 用的版本是struts2.3.1.1 一个简单的Struts项目所需的jar包有如下8个 1. struts2-core-2.3.1.1.jar: Struts2的核心类库. 2. ...

  4. java中常用jar包

    commons-io.jar:可以看成是java.io的扩展,用来帮助进行IO功能开发.它包含三个主要的领域:Utilityclasses-提供一些静态方法来完成公共任务.Filters-提供文件过滤 ...

  5. java 项目中每个jar包的作用总结

    别人的总结 1.Struts2 1)commons-fileupload :2)common-io:文件上传 3)commons-lang:它扩展了标准 java.langAPI ArrayUtils ...

  6. java Http post请求发送json字符串

    最近差点被业务逻辑搞懵逼,果然要先花时间思考,确定好流程再执行.目前最好用的jar包还是org.apache.http. public class HttpClientHelper { private ...

  7. Java HTTP 组件库选型看这篇就够了

    最近项目需要使用 Java 重度调用 HTTP API 接口,于是想着封装一个团队公用的 HTTP client lib. 这个库需要支持以下特性: 连接池管理,包括连接创建和超时.空闲连接数控制.每 ...

  8. Java开发新闻管理系统(前后端)+爬虫百度、新浪等新闻

                  ForFuture News  新闻管理系统                      项目演示地址:http://www.ganquanzhong.top [注]:文档下 ...

  9. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

随机推荐

  1. Oracle数据库Linux下的导入IMP

    和相关篇的EXP相对应的用了如下的导入方法. [oracle@localhost ~]$ imp Import: Release 11.2.0.1.0 - Production on Fri Sep ...

  2. php遇见的错误(一)

    1.linux 执行脚本时报的错 Call to a member function on a non-object in    是没有实例化对象 解决方法 new类2.在给一个表增加一个新字段或者改 ...

  3. cloudera安装报错 socket.gaierror: [Errno -2] Name or service not known

    有时候安装cloudera会报socket.gaierror: [Errno -2] Name or service not known,或者服务器可能IP或mac地址冲突会引发次错误.导致机器服务不 ...

  4. POJ 1966 Cable TV Network(顶点连通度的求解)

                               Cable TV Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  5. java的nio之:java的nio系列教程之serverSocketChannel

    Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样.ServerSocketChannel类在 jav ...

  6. php strtotime 和 date 日期操作

    time()得到的数值是1970-1-1到现在的秒数,strtotime("now")也是,两个值是相同的. http://doc.thinkphp.cn/manual/syste ...

  7. javascript encode或者decode html

    function myHTMLEnCode(str) { var s = ""; if (str.length == 0) return ""; s = str ...

  8. SVG 箭头线绘制

    SVG并没有提供原生的Arrow标签,这就需要自己的组合了,通过marker标签和path标签可以完美的模仿出箭头线,无论需要多少个箭头线,只需引用同一个marker即可: <svg id=&q ...

  9. 【转】CSS中怎么让DIV居中

    来源:http://www.cnblogs.com/DebugLZQ/archive/2011/08/09/2132381.html     CSS 如何使DIV层水平居中 今天用CSS碰到个很棘手的 ...

  10. JS对日期时间的操作

    代码: //判断是否超期(有效期开始超过一年后算已超期) function IsEffect(effectDate) { var val = ""; var currentDate ...