当前位置:网站首页>URLEncode. The difference between encode (string, string) and new string (byte[], string)
URLEncode. The difference between encode (string, string) and new string (byte[], string)
2022-07-22 01:56:00 【X ⑧】
URLEncode.encode(String,String) and new String(byte[],String) The difference between
The problem I have : Export Excel Put the form in HttpServletResponse When returning from , Use URLEncode.encode(String,String) Handle file name ;
and Local export Excel Use new String(byte[],String) Process filename ; What is the difference between the two ?
URLEncode.encode(String,String)
Parameters : The first parameter is : To transcode
character string
, The second parameter is : wantThe name of the specified encoding
( The compiler will prompt you for any mistakes ).String according to the specified encoding
transcoding
become URL code .URL Coding is actually : One
character ASCII The hexadecimal of the code
. For any non ASCII Special characters for ( Such as Chinese characters 、&、%、 Space 、=) To deal with , WithPercent sign % Add hexadecimal code
Express .Such as ‘%’ use URL The code is expressed as :‘%25’,
Space Expressed as :'+'( Special symbols )
,‘%25’、‘%E6’、‘%5C’ It's all one URL Encoded characters .
@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();
}
}
- For ordinary English letters, the result of transcoding is unchanged , Or the original character ; and Chinese characters stay
UTF-8
The middle isThree bytes, one Chinese character
To store the , After transcoding URL Coding is based onThree URL character
To express .
@Test
void text2() {
try {
System.out.println(URLEncoder.encode(" I ", "UTF-8"));// %E6%88%91
System.out.println(URLEncoder.encode(" yes ", "UTF-8"));// %E6%98%AF
System.out.println(URLEncoder.encode(" in ", "UTF-8"));// %E4%B8%AD
System.out.println(URLEncoder.encode(" countries ", "UTF-8"));// %E5%9B%BD
System.out.println(URLEncoder.encode(" people ", "UTF-8"));// %E4%BA%BA
System.out.println(Arrays.toString(" I ".getBytes(StandardCharsets.UTF_8)));// [-26, -120, -111]
System.out.println(" I ".getBytes(StandardCharsets.UTF_8).length); // 3
System.out.println(URLEncoder.encode(" I ", "UTF-8").length());// 9
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Test
void text3() {
String str = " I'm Chinese !";
String str1 = "I am Chinese!";
String str2 = " I'm Chinese !";
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)); // I'm Chinese !
System.out.println(new String(str1.getBytes(), StandardCharsets.UTF_8));// I am Chinese!
System.out.println(new String(str2.getBytes(), StandardCharsets.UTF_8));// I'm Chinese !
System.out.println(new String(str3.getBytes(), StandardCharsets.UTF_8));// I am Chinese!
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
new String(byte[],String)
- Parameters : The first parameter is : Bytecode array ; The second parameter is : want
The name of the specified encoding
, Recommended StandardCharsets Class to represent , Such asStandardCharsets.UTF_8 namely "UTF-8"
.
@Test
void text4() {
String str = " I'm Chinese !";
String str1 = "I am Chinese!";
String str2 = " I'm Chinese !";
String str3 = "I am Chinese!";
System.out.println(new String(str.getBytes(), StandardCharsets.UTF_8)); // I'm Chinese !
System.out.println(new String(str1.getBytes(), StandardCharsets.UTF_8));// I am Chinese!
System.out.println(new String(str2.getBytes(), StandardCharsets.UTF_8));// I'm Chinese !
System.out.println(new String(str3.getBytes(), StandardCharsets.UTF_8));// I am Chinese!
}
- getBytes(String): be used for
Converts a string into a bytecode array according to the specified encoding
, The parameter is the same as the second parameter of the method above ,The name of the specified encoding
, When there is no parameter , Applicable to the default character set of the platform code .
System.out.println(Arrays.toString(" I ".getBytes(StandardCharsets.UTF_8)));// [-26, -120, -111]
System.out.println(Arrays.toString(" I ".getBytes()));// [-26, -120, -111]
- Encode bytecode as specified
Regenerate a string
.
summary
The results are different
:new String(byte[],String) The result is still a string ; and URLEncode.encode(String,String) The result is URL Coding string , Just to String Fashion show .URL code : yes
A browser used to package form input , Text format transmitted between server and browser
. Commonly used to HttpServletResponse Return the filename ( You need to put HttpServletResponse As a file output stream ).new String(byte[],String) The method is to
Change the string encoding
.URLEncode.encode(String,String) The method is to
Transcoding a string
.
( Second engine ) If it helps you , Like it !!
边栏推荐
- Unity 之UGUI Text组件坚排显示(改进)
- Multiple knapsack problem code template
- 如何在 Kubernetes 集群中集成 Kata
- [paper reading] deep video analog detection: opportunities and challenges
- 如何用Go实现一个异步网络库?
- Oracle中对空字符串的判断
- The second financial article failed
- Learning path PHP -- thinkphp5 + windows server to achieve scheduled tasks
- Leetcode 206反转链表、3无重复字符的最长子串、912排序数组(快排)、215数组中的第k个最大元素、53最大子数组和、152乘积最大子数组
- Est - ce que cela signifie que le binlog de MySQL est activé?
猜你喜欢
一文深入浅出理解国产开源木兰许可系列协议
[paper reading] deep video analog detection: opportunities and challenges
Dynamic rules - array compression optimization
Leetcode 48旋转图像(水平+主对角线)、Leetcode 221最大正方形(动态规划dp表示以ij为右下角的答案值)、Leetcode 240搜索二维矩阵II(排除行列法)
Easyexcel realizes file upload - batch insert file download
c#利用resx manager 切换中英文
动态递归之严格位置依赖优化
xshell连接时显示“服务器发送了一个意外的数据包。received:3,expected:20
将华为路由器做成ftp服务器(实现上传下载功能)
第3章业务功能开发(实现显示线索主页面,并查询表单各个下拉框数据)
随机推荐
BGP—— 边界网关协议
Major breakthrough! Successful development of the first domestic scientific computing software
QT大杂烩(总)
2022-7-17 FTP client project implementation - Summary
解决GD32F207串口可以接收但发送00的问题
Group knapsack problem
TinyMCE removes the P tag added by default in the editor line feed
3. Project structure of source code analysis of Nacos configuration center
基于php开发的学生成绩管理系统
huawei设置使用账号密码登录
Jupyterhub configuring go environment
YouTube “标签产品” 试点项目上线
[深度学习学习笔记]注意力机制-Attentional mechanism
对比市面淘宝短视频工具/软件,分析淘宝短视频未来趋势
CRM concept: understand the concepts of leads, prospect, MQL and SQL
URLEncode.encode(String,String) 和 new String(byte[],String) 的区别
分组背包问题
C getting started series (XXVI) -- assemblies and namespaces
【FreeRTOS】10 事件标志组
SparkSql中的窗口函数