URLEncode.encode(String,String) 和 new String(byte[],String) 的区别
我遇到的问题:将导出 Excel 表格放到 HttpServletResponse 中返回时,使用 URLEncode.encode(String,String) 处理 文件名;
而 本地导出 Excel 表格时要用 new String(byte[],String) 处理文件名;两者到底有什么区别?
URLEncode.encode(String,String)
-
参数:第一个参数是:要转码的
字符串
,第二个参数是:要指定的编码的名称
(编译器会提示你有没有写错的)。 -
按照指定的编码将字符串
转码
成 URL 编码。 -
URL 编码其实是:一个
字符 ASCII 码的十六进制
。对任何非 ASCII 的特殊字符(如 汉字、&、%、空格、=)进行处理,以百分号%加十六进制编码
表示。 -
如 ‘%’ 用 URL 编码表示为:‘%25’,
空格 表示为:'+'(特殊符号)
,‘%25’、‘%E6’、‘%5C’ 都是一个 URL 编码字符。
@Test
void text1() {
try {
System.out.println(URLEncoder.encode(" ", "UTF-8")); // +
System.out.println(URLEncoder.encode("!", "UTF-8"));// %EF%BC%81
System.out.println(URLEncoder.encode("!", "UTF-8")); // %21
System.out.println(URLEncoder.encode("/", "UTF-8")); // %2F
System.out.println(URLEncoder.encode("\\", "UTF-8"));// %5C
System.out.println(URLEncoder.encode("%", "UTF-8")); // %25
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
- 对于普通的英文字母转码后的结果是不变的,还是原字符;而 汉字 在
UTF-8
中是以三个字节一个汉字
来存储的,转码后的 URL 编码则是以三个 URL 字符
来表示。
@Test
void text2() {
try {
System.out.println(URLEncoder.encode("我", "UTF-8"));// %E6%88%91
System.out.println(URLEncoder.encode("是", "UTF-8"));// %E6%98%AF
System.out.println(URLEncoder.encode("中", "UTF-8"));// %E4%B8%AD
System.out.println(URLEncoder.encode("国", "UTF-8"));// %E5%9B%BD
System.out.println(URLEncoder.encode("人", "UTF-8"));// %E4%BA%BA
System.out.println(Arrays.toString("我".getBytes(StandardCharsets.UTF_8)));// [-26, -120, -111]
System.out.println("我".getBytes(StandardCharsets.UTF_8).length); // 3
System.out.println(URLEncoder.encode("我", "UTF-8").length());// 9
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Test
void text3() {
String str = "我是中国人!";
String str1 = "I am Chinese!";
String str2 = "我是中国人!";
String str3 = "I am Chinese!";
try {
System.out.println(URLEncoder.encode(str, "UTF-8")); // %E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA%EF%BC%81
System.out.println(URLEncoder.encode(str1, "UTF-8"));// I+am+Chinese%EF%BC%81
System.out.println(URLEncoder.encode(str2, "UTF-8"));// %E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA%21
System.out.println(URLEncoder.encode(str3, "UTF-8"));// I+am+Chinese%21
System.out.println(new String(str.getBytes(), StandardCharsets.UTF_8)); // 我是中国人!
System.out.println(new String(str1.getBytes(), StandardCharsets.UTF_8));// I am Chinese!
System.out.println(new String(str2.getBytes(), StandardCharsets.UTF_8));// 我是中国人!
System.out.println(new String(str3.getBytes(), StandardCharsets.UTF_8));// I am Chinese!
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
new String(byte[],String)
- 参数:第一个参数是:字节码数组;第二个参数是:要
指定的编码的名称
,推荐使用 StandardCharsets 类的属性来表示,如StandardCharsets.UTF_8 即 "UTF-8"
。
@Test
void text4() {
String str = "我是中国人!";
String str1 = "I am Chinese!";
String str2 = "我是中国人!";
String str3 = "I am Chinese!";
System.out.println(new String(str.getBytes(), StandardCharsets.UTF_8)); // 我是中国人!
System.out.println(new String(str1.getBytes(), StandardCharsets.UTF_8));// I am Chinese!
System.out.println(new String(str2.getBytes(), StandardCharsets.UTF_8));// 我是中国人!
System.out.println(new String(str3.getBytes(), StandardCharsets.UTF_8));// I am Chinese!
}
- getBytes(String):用来
将字符串按照指定的编码转换为字节码数组
,参数与上面的方法的第二个参数一样,指定的编码的名称
,无参数时,适用平台默认的字符集 编码。
System.out.println(Arrays.toString("我".getBytes(StandardCharsets.UTF_8)));// [-26, -120, -111]
System.out.println(Arrays.toString("我".getBytes()));// [-26, -120, -111]
- 将字节码按照指定的编码
重新生成一个字符串
。
总结
-
结果不同
:new String(byte[],String) 的结果还是字符串;而 URLEncode.encode(String,String) 的结果是 URL 编码串,只是以 String 的方式展示。 -
URL 编码:是
一种浏览器用来打包表单输入,在服务器与浏览器之间传输的文本格式
。常用来向 HttpServletResponse 返回文件名(需要同时将 HttpServletResponse 作为文件输出流)。 -
new String(byte[],String) 方法是用来
更改字符串编码的
。 -
URLEncode.encode(String,String) 方法是用来
将字符串转码的
。
(二发)如果对你有帮助,点赞可好!!