代码优化 UTF-8

This commit is contained in:
thinkgem
2021-05-29 00:01:07 +08:00
parent 86ffdf24b8
commit 9403efc790
23 changed files with 81 additions and 69 deletions

View File

@@ -27,7 +27,6 @@ public class AesUtils {
private static final int DEFAULT_IVSIZE = 16; // 生成随机向量, 默认大小为cipher.getBlockSize(), 16字节
private static final SecureRandom RANDOM = new SecureRandom(); // 用于 生成 generateIV随机数对象
private static final String DEFAULT_URL_ENCODING = "UTF-8";
private static final byte[] DEFAULT_KEY = new byte[]{-97,88,-94,9,70,-76,126,25,0,3,-20,113,108,28,69,125};
/**
@@ -44,7 +43,7 @@ public class AesUtils {
*/
public static String encode(String input) {
try {
return EncodeUtils.encodeHex(encode(input.getBytes(DEFAULT_URL_ENCODING), DEFAULT_KEY));
return EncodeUtils.encodeHex(encode(input.getBytes(EncodeUtils.UTF_8), DEFAULT_KEY));
} catch (UnsupportedEncodingException e) {
return "";
}
@@ -58,7 +57,7 @@ public class AesUtils {
*/
public static String encode(String input, String key) {
try {
return EncodeUtils.encodeHex(encode(input.getBytes(DEFAULT_URL_ENCODING), EncodeUtils.decodeHex(key)));
return EncodeUtils.encodeHex(encode(input.getBytes(EncodeUtils.UTF_8), EncodeUtils.decodeHex(key)));
} catch (UnsupportedEncodingException e) {
return "";
}
@@ -71,7 +70,7 @@ public class AesUtils {
*/
public static String decode(String input) {
try {
return new String(decode(EncodeUtils.decodeHex(input), DEFAULT_KEY), DEFAULT_URL_ENCODING);
return new String(decode(EncodeUtils.decodeHex(input), DEFAULT_KEY), EncodeUtils.UTF_8);
} catch (UnsupportedEncodingException e) {
return "";
}
@@ -85,7 +84,7 @@ public class AesUtils {
*/
public static String decode(String input, String key) {
try {
return new String(decode(EncodeUtils.decodeHex(input), EncodeUtils.decodeHex(key)), DEFAULT_URL_ENCODING);
return new String(decode(EncodeUtils.decodeHex(input), EncodeUtils.decodeHex(key)), EncodeUtils.UTF_8);
} catch (UnsupportedEncodingException e) {
return "";
}

View File

@@ -36,8 +36,9 @@ import com.jeesite.common.lang.StringUtils;
*/
public class EncodeUtils {
public static final String UTF_8 = "UTF-8";
private static final Logger logger = LoggerFactory.getLogger(EncodeUtils.class);
private static final String DEFAULT_URL_ENCODING = "UTF-8";
private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
/**
@@ -73,7 +74,7 @@ public class EncodeUtils {
return StringUtils.EMPTY;
}
try {
return new String(Base64.encodeBase64(input.getBytes(DEFAULT_URL_ENCODING)));
return new String(Base64.encodeBase64(input.getBytes(EncodeUtils.UTF_8)));
} catch (UnsupportedEncodingException e) {
return "";
}
@@ -90,7 +91,11 @@ public class EncodeUtils {
* Base64解码.
*/
public static byte[] decodeBase64(String input) {
return Base64.decodeBase64(input.getBytes());
try {
return Base64.decodeBase64(input.getBytes(EncodeUtils.UTF_8));
} catch (UnsupportedEncodingException e) {
throw ExceptionUtils.unchecked(e);
}
}
/**
@@ -101,7 +106,7 @@ public class EncodeUtils {
return StringUtils.EMPTY;
}
try {
return new String(Base64.decodeBase64(input.getBytes()), DEFAULT_URL_ENCODING);
return new String(Base64.decodeBase64(input.getBytes(EncodeUtils.UTF_8)), EncodeUtils.UTF_8);
} catch (UnsupportedEncodingException e) {
return StringUtils.EMPTY;
}
@@ -150,7 +155,7 @@ public class EncodeUtils {
* URL 编码, Encode默认为UTF-8.
*/
public static String encodeUrl(String part) {
return encodeUrl(part, DEFAULT_URL_ENCODING);
return encodeUrl(part, EncodeUtils.UTF_8);
}
/**
@@ -171,7 +176,7 @@ public class EncodeUtils {
* URL 解码, Encode默认为UTF-8.
*/
public static String decodeUrl(String part) {
return decodeUrl(part, DEFAULT_URL_ENCODING);
return decodeUrl(part, EncodeUtils.UTF_8);
}
/**

View File

@@ -20,7 +20,6 @@ import com.jeesite.common.io.IOUtils;
public class Md5Utils {
private static final String MD5 = "MD5";
private static final String DEFAULT_ENCODING = "UTF-8";
/**
* 对输入字符串进行md5散列.
@@ -37,7 +36,7 @@ public class Md5Utils {
*/
public static String md5(String input, int iterations) {
try {
return EncodeUtils.encodeHex(DigestUtils.digest(input.getBytes(DEFAULT_ENCODING), MD5, null, iterations));
return EncodeUtils.encodeHex(DigestUtils.digest(input.getBytes(EncodeUtils.UTF_8), MD5, null, iterations));
} catch (UnsupportedEncodingException e) {
return StringUtils.EMPTY;
}

View File

@@ -22,7 +22,6 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -244,7 +243,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
*/
public static String readFileToString(String classResourcePath){
try (InputStream in = new ClassPathResource(classResourcePath).getInputStream()){
return IOUtils.toString(in, Charsets.toCharset("UTF-8"));
return IOUtils.toString(in, EncodeUtils.UTF_8);
} catch (IOException e) {
logger.warn("Error file convert: {}", e.getMessage());
}
@@ -422,7 +421,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
*/
public static void writeToFile(String fileName, String content, boolean append) {
try {
FileUtils.write(new File(fileName), content, "utf-8", append);
FileUtils.write(new File(fileName), content, EncodeUtils.UTF_8, append);
logger.debug("文件 " + fileName + " 写入成功!");
} catch (IOException e) {
logger.debug("文件 " + fileName + " 写入失败! " + e.getMessage());

View File

@@ -18,6 +18,7 @@ import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.collect.SetUtils;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.lang.StringUtils;
@@ -111,7 +112,7 @@ public class PropertiesUtils {
Resource resource = ResourceUtils.getResource(location);
if (resource.exists()){
if (location.endsWith(".properties")){
try (InputStreamReader is = new InputStreamReader(resource.getInputStream(), "UTF-8")){
try (InputStreamReader is = new InputStreamReader(resource.getInputStream(), EncodeUtils.UTF_8)){
properties.load(is);
configSet.add(location);
} catch (IOException e) {

View File

@@ -12,6 +12,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.lang.ExceptionUtils;
/**
@@ -68,7 +69,7 @@ public class ResourceUtils extends org.springframework.util.ResourceUtils {
*/
public static String getResourceFileContent(String location){
try(InputStream is = ResourceUtils.getResourceFileStream(location)){
return IOUtils.toString(is, "UTF-8");
return IOUtils.toString(is, EncodeUtils.UTF_8);
}catch (IOException e) {
throw ExceptionUtils.unchecked(e);
}

View File

@@ -20,7 +20,6 @@ import com.jeesite.common.collect.ListUtils;
public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final char SEPARATOR = '_';
private static final String CHARSET_NAME = "UTF-8";
/**
* 转换为字节数组
@@ -30,7 +29,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
public static byte[] getBytes(String str){
if (str != null){
try {
return str.getBytes(CHARSET_NAME);
return str.getBytes(EncodeUtils.UTF_8);
} catch (UnsupportedEncodingException e) {
return null;
}
@@ -46,7 +45,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
*/
public static String toString(byte[] bytes){
try {
return new String(bytes, CHARSET_NAME);
return new String(bytes, EncodeUtils.UTF_8);
} catch (UnsupportedEncodingException e) {
return EMPTY;
}

View File

@@ -7,6 +7,7 @@ import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.io.PropertiesUtils;
/**
@@ -66,7 +67,7 @@ public class EmailUtils {
htmlEmail.setMsg(content);
// 其他信息
htmlEmail.setCharset("utf-8");
htmlEmail.setCharset(EncodeUtils.UTF_8);
// 发送
htmlEmail.send();

View File

@@ -34,7 +34,7 @@ public class SmsUtils {
// dataMap.put("mobile", mobile);
// // 短信内容
// dataMap.put("content", prefix + content + suffix);
// HttpClientUtils.post(url, dataMap, "UTF-8");
// HttpClientUtils.post(url, dataMap, EncodeUtils.UTF_8);
logger.debug("短信内容:" + content + " 手机号码:" + mobile);
logger.warn("短信模拟发送成功!实际并未发送到手机,请实现 " + SmsUtils.class + " 的 send 方法。");
return "{result:0,message:\"短信模拟发送成功!\"}";

View File

@@ -29,6 +29,8 @@ import java.util.jar.JarInputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.jeesite.common.codec.EncodeUtils;
/**
* Class工具类借鉴ibatis的io包的ResolverUtil类。
* @author ThinkGem
@@ -620,7 +622,7 @@ class DefaultVFS extends VFS {
// File name might be URL-encoded
if (!file.exists()) {
try {
file = new File(URLEncoder.encode(jarUrl.toString(), "UTF-8"));
file = new File(URLEncoder.encode(jarUrl.toString(), EncodeUtils.UTF_8));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding? UTF-8? That's unpossible.");
}

View File

@@ -19,8 +19,8 @@
package com.jeesite.common.text;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -32,6 +32,8 @@ import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.jeesite.common.codec.EncodeUtils;
/*
* Functions for diff, match and patch.
* Computes the difference between two texts to create a patch.
@@ -1433,7 +1435,7 @@ public class DiffMatchPatch {
switch (aDiff.operation) {
case INSERT:
try {
text.append("+").append(URLEncoder.encode(aDiff.text, "UTF-8")
text.append("+").append(URLEncoder.encode(aDiff.text, EncodeUtils.UTF_8)
.replace('+', ' ')).append("\t");
} catch (UnsupportedEncodingException e) {
// Not likely on modern system.
@@ -1483,7 +1485,7 @@ public class DiffMatchPatch {
// decode would change all "+" to " "
param = param.replace("+", "%2B");
try {
param = URLDecoder.decode(param, "UTF-8");
param = URLDecoder.decode(param, EncodeUtils.UTF_8);
} catch (UnsupportedEncodingException e) {
// Not likely on modern system.
throw new Error("This system does not support UTF-8.", e);
@@ -2255,7 +2257,7 @@ public class DiffMatchPatch {
line = text.getFirst().substring(1);
line = line.replace("+", "%2B"); // decode would change all "+" to " "
try {
line = URLDecoder.decode(line, "UTF-8");
line = URLDecoder.decode(line, EncodeUtils.UTF_8);
} catch (UnsupportedEncodingException e) {
// Not likely on modern system.
throw new Error("This system does not support UTF-8.", e);
@@ -2424,7 +2426,7 @@ public class DiffMatchPatch {
break;
}
try {
text.append(URLEncoder.encode(aDiff.text, "UTF-8").replace('+', ' '))
text.append(URLEncoder.encode(aDiff.text, EncodeUtils.UTF_8).replace('+', ' '))
.append("\n");
} catch (UnsupportedEncodingException e) {
// Not likely on modern system.

View File

@@ -18,6 +18,7 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.image.ImageUtils;
import com.jeesite.common.io.FileUtils;
import com.jeesite.common.media.VideoUtils;
@@ -43,7 +44,7 @@ public class BinaryUploader {
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
if ( isAjaxUpload ) {
upload.setHeaderEncoding( "UTF-8" );
upload.setHeaderEncoding( EncodeUtils.UTF_8 );
}
try {

View File

@@ -32,6 +32,8 @@ import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import com.jeesite.common.codec.EncodeUtils;
/**
* HTTP客户端工具类支持HTTPS
* @author ThinkGem
@@ -44,7 +46,7 @@ public class HttpClientUtils {
* @param url
*/
public static String get(String url) {
return get(url, "UTF-8");
return get(url, EncodeUtils.UTF_8);
}
/**
@@ -61,7 +63,7 @@ public class HttpClientUtils {
* @param url
*/
public static String ajaxGet(String url) {
return ajaxGet(url, "UTF-8");
return ajaxGet(url, EncodeUtils.UTF_8);
}
/**
@@ -78,7 +80,7 @@ public class HttpClientUtils {
* http的post请求传递map格式参数
*/
public static String post(String url, Map<String, String> dataMap) {
return post(url, dataMap, "UTF-8");
return post(url, dataMap, EncodeUtils.UTF_8);
}
/**
@@ -106,7 +108,7 @@ public class HttpClientUtils {
* http的post请求增加异步请求头参数传递map格式参数
*/
public static String ajaxPost(String url, Map<String, String> dataMap) {
return ajaxPost(url, dataMap, "UTF-8");
return ajaxPost(url, dataMap, EncodeUtils.UTF_8);
}
/**
@@ -135,7 +137,7 @@ public class HttpClientUtils {
* http的post请求增加异步请求头参数传递json格式参数
*/
public static String ajaxPostJson(String url, String jsonString) {
return ajaxPostJson(url, jsonString, "UTF-8");
return ajaxPostJson(url, jsonString, EncodeUtils.UTF_8);
}
/**
@@ -159,7 +161,7 @@ public class HttpClientUtils {
* 执行一个http请求传递HttpGet或HttpPost参数
*/
public static String executeRequest(HttpUriRequest httpRequest) {
return executeRequest(httpRequest, "UTF-8");
return executeRequest(httpRequest, EncodeUtils.UTF_8);
}
/**