当前位置:网站首页>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 : want The 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 , With Percent 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 is Three bytes, one Chinese character To store the , After transcoding URL Coding is based on Three 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 as StandardCharsets.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 !!

原网站

版权声明
本文为[X ⑧]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/203/202207210833334058.html