Chinese character encoding?

I have a use case where I send parameters to the spring controller via an email request. In the controller, I read the parameters and perform some actions. After that, I send these parameters as part of the request parameters to another URL.

Here I can not deal with Chinese characters. He becomes distorted.

The actions that I am doing now: 1) I go under the Chinese text as a parameter with the name of the topic from the HTML page (this is not JSP). 以下 便是 有关 此 问题 的 所有 信息

2) When I read this value from the request in the controller, it comes as: ä "¥ ä¸ä¾¿æ¯æå³æ¤é®é ¢ çææä¿¡æ¯

3) I can not get the exact value that is sent from the page.

It looks like it is already encoded when I check the encoded text below the url: http://coderstoolbox.net/string/#!encoding=none&action=encode&charset=utf_8 http://www.cafewebmaster.com/online_tools/utf_decode

4) Now I want to pass the actual string sent by the user to another URL as response.sendRedirect. I tried to decrypt the url to find out if I can get the actual string, but did not succeed.

I am using tomcat server. I defined UTF-8 encoding in server.xml and added URLEncodingFilter in web.xml as the first filter. This filter executes request.setEncoding for UTF-8.

However, I cannot keep track of where everything is going wrong. Can someone suggest me how to return the actual string to the controller?

Also there is a filter below in my web.xml

<filter> <filter-name>EncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> 

Let me know if you need any information to get more context.

+4
source share
4 answers

If you are using, please change the Connector in the server.xml file below

 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"> </Connector> 

Hope this solves your problem.

Regards, Kishor

+2
source

try adding this filter to your web.xml :

 <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> 

and display it:

 <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

I had a similar problem and this solved it.

0
source

After doing the decoding in the following, I can get the actual string. I am still investigating why I need to do Latin decoding. I will update as soon as I get a full understanding of the problem. If any of you know the reason for the Latin encoding, let me know.

 public String getncodedSubject(String text) { if (text == null || text.isEmpty()) { return ""; } try { byte[] encoding1 = subject.getBytes("UTF-8"); String string1 = new String(encoding1, 0, encoding1.length); // Default encoding of my platform is UTF-8 byte[] encoding2 = string1.getBytes("ISO8859-1");//ISO-8859-1 (ISO Latin 1) Character Encoding char[] hexaChars = Hex.encodeHex(encoding2); StringBuilder str = new StringBuilder(); for(int i=0;i<hexaChars.length;i = i+2){ str.append("%"); str.append(hexaChars[i]); str.append(hexaChars[i+1]); } return str.toString(); } catch (UnsupportedEncodingException e) { System.out.println(e); } return ""; } 

After digging, it seems more like he is getting a Latin encoded string:

 import java.nio.charset.CharsetDecoder; import java.nio.charset.Charset; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { byte[] encoding1 = "以ä¸ä¾¿æ¯æå³æ¤é®é¢çææä¿¡æ¯".getBytes("ISO8859-1"); for (byte b : encoding1) { System.out.printf("%x ",b); } } } 

However, I'm not sure how it gets the Latin encoded string ... any suggestions? I also checked on my .xml server

0
source

Thank you all for your answers. The following are my observations.

I view my page using Mason (Perl + HTML), not using JSP. Therefore, I could not specify the type of encoding on the page in order to force the browser to send the UTF-8 encoded string.

Now I programmatically decode using "ISO8859-1" (Latin) and encode UTF-8 to get the actual string for consumption.

Please let me know how to specify the encoding type in Mason (Perl + HTML) so that it passes parameters with UTF-8 encoding instead of accepting standard encoding.

 import java.nio.charset.CharsetDecoder; import java.nio.charset.Charset; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { byte[] encoding1 = "以下便是有关æ¤é—®é¢˜çš„æ‰€æœ‰ä¿¡æ ¯".getBytes("ISO8859-1"); String s = new String(encoding1, "UTF-8"); System.out.println(s); } } 
-one
source

Source: https://habr.com/ru/post/1496823/


All Articles