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");
}
}
CharsetUtils.java的更多相关文章
- Spark案例分析
		一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ... 
- 【java】org.apache.commons.lang3功能示例
		org.apache.commons.lang3功能示例 package com.simple.test; import java.util.Date; import java.util.Iterat ... 
- java项目中可能会使用到的jar包解释
		一.Struts2 用的版本是struts2.3.1.1 一个简单的Struts项目所需的jar包有如下8个 1. struts2-core-2.3.1.1.jar: Struts2的核心类库. 2. ... 
- java中常用jar包
		commons-io.jar:可以看成是java.io的扩展,用来帮助进行IO功能开发.它包含三个主要的领域:Utilityclasses-提供一些静态方法来完成公共任务.Filters-提供文件过滤 ... 
- java 项目中每个jar包的作用总结
		别人的总结 1.Struts2 1)commons-fileupload :2)common-io:文件上传 3)commons-lang:它扩展了标准 java.langAPI ArrayUtils ... 
- java Http post请求发送json字符串
		最近差点被业务逻辑搞懵逼,果然要先花时间思考,确定好流程再执行.目前最好用的jar包还是org.apache.http. public class HttpClientHelper { private ... 
- Java HTTP 组件库选型看这篇就够了
		最近项目需要使用 Java 重度调用 HTTP API 接口,于是想着封装一个团队公用的 HTTP client lib. 这个库需要支持以下特性: 连接池管理,包括连接创建和超时.空闲连接数控制.每 ... 
- Java开发新闻管理系统(前后端)+爬虫百度、新浪等新闻
		ForFuture News 新闻管理系统 项目演示地址:http://www.ganquanzhong.top [注]:文档下 ... 
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
		背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ... 
随机推荐
- linux中socket的理解---4
			一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用“打 开open –> 读写write/read –> 关闭close”模式来操作.So ... 
- UVa 572 油田(DFS求连通块)
			https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ... 
- Eclipse中web-inf和meta-inf文件夹的信息
			http://www.cnblogs.com/chinafine/archive/2010/06/13/1757514.html WEB-INF /WEB-INF/web.xml ... 
- Java设计模式之责任链设计模式
			职责链模式(Chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将所有处理对象连成一条链,并沿着这条链传递请求,直到有一个对象处理 ... 
- STL 简介,标准模板库
			这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL. 当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ... 
- Too many authentic authentication failures for root
			连接SSH服务器时报 Too many authentic authentication failures for root. 方法一:删除目标服务器 /root/.ssh/目录下的 known_ho ... 
- JSBinding + SharpKit / 原理篇:内存管理与垃圾回收
			C# 和 JS 都有垃圾回收机制,需要保证 2 者能够分工协作. 类对象 类在C#中是引用类型.我们在 C# 中维护了2个map,保存 C# 对象和 JS 对象的一一对应关系. 举一个例子,看以下代码 ... 
- 让边框和文本一样高,不受line-height影响,可以使用padding,padding可以用于行内元素
			如果设置了,display:inline-block;边框大小至少和行高一样.这样就可能比文字高. 
- 采用libsvm进行mnist训练
			#coding:utf8 import cPickle import gzip import numpy as np from sklearn.svm import libsvm class SVM( ... 
- entity framework里的继承映射关系TPH、TPT和TPC
			本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discr ... 
