mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add Request
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
@@ -36,13 +43,6 @@ import java.util.zip.CRC32;
|
||||
import java.util.zip.CheckedInputStream;
|
||||
import java.util.zip.Checksum;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* IO工具类<br>
|
||||
* IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
|
||||
@@ -453,7 +453,7 @@ public class IoUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 从流中读取内容,读到输出流中
|
||||
* 从流中读取内容,读到输出流中,读取完毕后并不关闭流
|
||||
*
|
||||
* @param in 输入流
|
||||
* @return 输出流
|
||||
|
@@ -1,65 +1,102 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
|
||||
/**
|
||||
* 字符集工具类
|
||||
* @author xiaoleilu
|
||||
*
|
||||
* @author xiaoleilu
|
||||
*/
|
||||
public class CharsetUtil {
|
||||
|
||||
/** ISO-8859-1 */
|
||||
|
||||
/**
|
||||
* ISO-8859-1
|
||||
*/
|
||||
public static final String ISO_8859_1 = "ISO-8859-1";
|
||||
/** UTF-8 */
|
||||
/**
|
||||
* UTF-8
|
||||
*/
|
||||
public static final String UTF_8 = "UTF-8";
|
||||
/** GBK */
|
||||
/**
|
||||
* GBK
|
||||
*/
|
||||
public static final String GBK = "GBK";
|
||||
|
||||
/** ISO-8859-1 */
|
||||
|
||||
/**
|
||||
* ISO-8859-1
|
||||
*/
|
||||
public static final Charset CHARSET_ISO_8859_1 = StandardCharsets.ISO_8859_1;
|
||||
/** UTF-8 */
|
||||
/**
|
||||
* UTF-8
|
||||
*/
|
||||
public static final Charset CHARSET_UTF_8 = StandardCharsets.UTF_8;
|
||||
/** GBK */
|
||||
/**
|
||||
* GBK
|
||||
*/
|
||||
public static final Charset CHARSET_GBK;
|
||||
|
||||
static{
|
||||
static {
|
||||
//避免不支持GBK的系统中运行报错 issue#731
|
||||
Charset _CHARSET_GBK = null;
|
||||
try{
|
||||
try {
|
||||
_CHARSET_GBK = Charset.forName(GBK);
|
||||
} catch (UnsupportedCharsetException e){
|
||||
} catch (UnsupportedCharsetException e) {
|
||||
//ignore
|
||||
}
|
||||
CHARSET_GBK = _CHARSET_GBK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换为Charset对象
|
||||
*
|
||||
* @param charsetName 字符集,为空则返回默认字符集
|
||||
* @return Charset
|
||||
* @throws UnsupportedCharsetException 编码不支持
|
||||
*/
|
||||
public static Charset charset(String charsetName) throws UnsupportedCharsetException{
|
||||
public static Charset charset(String charsetName) throws UnsupportedCharsetException {
|
||||
return StrUtil.isBlank(charsetName) ? Charset.defaultCharset() : Charset.forName(charsetName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析字符串编码为Charset对象,解析失败返回默认编码
|
||||
*
|
||||
* @param charsetName 字符集,为空则返回默认字符集
|
||||
* @param defaultCharset 解析失败使用的默认编码
|
||||
* @return Charset
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static Charset parse(String charsetName, Charset defaultCharset) throws UnsupportedCharsetException {
|
||||
if (StrUtil.isBlank(charsetName)) {
|
||||
return defaultCharset;
|
||||
}
|
||||
|
||||
Charset result;
|
||||
try {
|
||||
result = Charset.forName(charsetName);
|
||||
} catch (UnsupportedCharsetException e) {
|
||||
result = defaultCharset;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换字符串的字符集编码
|
||||
* @param source 字符串
|
||||
* @param srcCharset 源字符集,默认ISO-8859-1
|
||||
*
|
||||
* @param source 字符串
|
||||
* @param srcCharset 源字符集,默认ISO-8859-1
|
||||
* @param destCharset 目标字符集,默认UTF-8
|
||||
* @return 转换后的字符集
|
||||
*/
|
||||
public static String convert(String source, String srcCharset, String destCharset) {
|
||||
return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换字符串的字符集编码<br>
|
||||
* 当以错误的编码读取为字符串时,打印字符串将出现乱码。<br>
|
||||
@@ -69,33 +106,33 @@ public class CharsetUtil {
|
||||
* 客户端 -》 GBK编码 -》 Servlet容器 -》 UTF-8解码 -》 乱码
|
||||
* 乱码 -》 UTF-8编码 -》 GBK解码 -》 正确内容
|
||||
* </pre>
|
||||
*
|
||||
* @param source 字符串
|
||||
* @param srcCharset 源字符集,默认ISO-8859-1
|
||||
*
|
||||
* @param source 字符串
|
||||
* @param srcCharset 源字符集,默认ISO-8859-1
|
||||
* @param destCharset 目标字符集,默认UTF-8
|
||||
* @return 转换后的字符集
|
||||
*/
|
||||
public static String convert(String source, Charset srcCharset, Charset destCharset) {
|
||||
if(null == srcCharset) {
|
||||
if (null == srcCharset) {
|
||||
srcCharset = StandardCharsets.ISO_8859_1;
|
||||
}
|
||||
|
||||
if(null == destCharset) {
|
||||
|
||||
if (null == destCharset) {
|
||||
destCharset = StandardCharsets.UTF_8;
|
||||
}
|
||||
|
||||
|
||||
if (StrUtil.isBlank(source) || srcCharset.equals(destCharset)) {
|
||||
return source;
|
||||
}
|
||||
return new String(source.getBytes(srcCharset), destCharset);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换文件编码<br>
|
||||
* 此方法用于转换文件编码,读取的文件实际编码必须与指定的srcCharset编码一致,否则导致乱码
|
||||
*
|
||||
* @param file 文件
|
||||
* @param srcCharset 原文件的编码,必须与文件内容的编码保持一致
|
||||
*
|
||||
* @param file 文件
|
||||
* @param srcCharset 原文件的编码,必须与文件内容的编码保持一致
|
||||
* @param destCharset 转码后的编码
|
||||
* @return 被转换编码的文件
|
||||
* @since 3.1.0
|
||||
@@ -104,41 +141,41 @@ public class CharsetUtil {
|
||||
final String str = FileUtil.readString(file, srcCharset);
|
||||
return FileUtil.writeString(str, file, destCharset);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 系统字符集编码,如果是Windows,则默认为GBK编码,否则取 {@link CharsetUtil#defaultCharsetName()}
|
||||
*
|
||||
* @see CharsetUtil#defaultCharsetName()
|
||||
*
|
||||
* @return 系统字符集编码
|
||||
* @see CharsetUtil#defaultCharsetName()
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public static String systemCharsetName() {
|
||||
return systemCharset().name();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 系统字符集编码,如果是Windows,则默认为GBK编码,否则取 {@link CharsetUtil#defaultCharsetName()}
|
||||
*
|
||||
* @see CharsetUtil#defaultCharsetName()
|
||||
*
|
||||
* @return 系统字符集编码
|
||||
* @see CharsetUtil#defaultCharsetName()
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public static Charset systemCharset() {
|
||||
return FileUtil.isWindows() ? CHARSET_GBK : defaultCharset();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 系统默认字符集编码
|
||||
*
|
||||
*
|
||||
* @return 系统字符集编码
|
||||
*/
|
||||
public static String defaultCharsetName() {
|
||||
return defaultCharset().name();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 系统默认字符集编码
|
||||
*
|
||||
*
|
||||
* @return 系统字符集编码
|
||||
*/
|
||||
public static Charset defaultCharset() {
|
||||
|
Reference in New Issue
Block a user