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引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
随机推荐
- Sql Server_笔记
1.随机取出10条数据:select top 10 * from tablename order by newid()
- C陷阱与缺陷 2
1,数组 对数组只能进行两种操作,1确定数组的大小,2获得数组第一个元素的指针,其他的操作均是通过指针来实现的. 1 2 3 4 5 6 7 8 9 #include <stdio.h> ...
- squid代理服务器搭建及配置
系统环境:CentOS release 6.5 (Final)(最小化安装) 一.安装squid # yum -y install squid 二.编辑配置文件(正向代理) # vim /etc/sq ...
- POJ 1469 COURSES(二部图匹配)
COURSES Time Limit: 1000MS Memory ...
- Error using subsindex Function 'subsindex' is not defined for values of class 'struct'.
clc; clear all; close all; image_path = '/media/wangxiao/Elements/image_segment_backup/'; savePath = ...
- 在OCR文字识别软件选项卡中怎么设置图像和文字
PDF是广泛使用的文档格式.在ABBYY Finereader中,PDF文档的显示不会因电脑不同而有差异,可加密保护,非常适合在电子存档中进行保存.下面给 大家讲解如何在PDF选项设置图像和文字. 图 ...
- 在Hyper-V虚拟机中使用Wi-Fi上网
笔记本配置了一块以太网卡和一块无线网卡.由于平时常用Wi-Fi上网,偶然发现Hyper-V虚拟机默认不能使用宿主系统的无线网卡上网,据说是出于安全方面的考虑.后来参考"Using Wirel ...
- C#4.0新特性:可选参数,命名参数,Dynamic
1.可选参数 可以为方法的参数设置一个默认值,如下: class Program { static void Main(string[] args) { Show(); Show("cary ...
- Web前端开发笔试&面试_04
>>XDL: 1.在CSS中,选择器的优先级?(如important,id,class 这些……) 2.如何消除行内间隙? Inline-block 3. 如何清除浮动? —— 4. CS ...
- rsyslog
http://www.rsyslog.com/ http://www.rsyslog.com/doc/v5-stable/troubleshooting/troubleshoot.html RSYSL ...