Home  >  Q&A  >  body text

Error decoding url

Problem: The Chinese decoded by Android to the background server is garbled. .

environment:

1. tomcat,在server.xml中已经明确了“utf-8".
2. 安卓已经明确了使用Utf-8编码
3. 在后台使用` java.net.URLDecoder.decode(url, "utf-8");`对url进行解码,但是解码出错。。中文是”??“。

It should be an encoding problem, but. . .

Many thanks. . .

In addition, I use the following statement to test the encoding:

if (url.equals(new String(url.getBytes("iso8859-1"), "iso8859-1"))) {
    url = new String(url.getBytes("iso8859-1"), "utf-8");
    System.out.println("ios8859-1");
}
if (url.equals(new String(url.getBytes("GB2312"), "GB2312"))) {
    url = new String(url.getBytes("GB2312"), "utf-8");
    System.out.println("gb2312");
}

Output

ios8859-1
gb2312

. . . .

曾经蜡笔没有小新曾经蜡笔没有小新2674 days ago773

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-06-23 09:15:56

    Has the encoding filter been added to the background?

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-23 09:15:56

    URLEncode/Decode is for the parameter values ​​in the URL to avoid conflicts with the URL format itself. Tomcat will automatically decode URL parameter values, so you just need to get them directly. For example, this JSP:

    <%@ page import="java.net.URLEncoder" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        System.out.println("name=" + request.getParameter("name"));
    %>
    <a href="?name=<%=URLEncoder.encode("你好", "UTF-8")%>">你好</a>
    <a href="?name=你好">你好</a>

    In fact, there is no difference between the two "Hello" links. You can open the source code of the web page to see their link addresses. One is encoded and the other is not. But after clicking, the console will output the correct value, which means request.getParameter("name") The content obtained is exactly the same.

    reply
    0
  • Cancelreply