简体   繁体   English

Tomcat8 UTF-8显示为问号

[英]Tomcat8 UTF-8 showing as question mark

I have read around for days but can't get this solved! 我已经阅读了几天,但无法解决!

Ok. 好。 I have a webapp that makes a bytearray based on GET parameters and sends it to a server. 我有一个Webapp,它基于GET参数制作一个字节数组,并将其发送到服务器。 The reply is then decrypted and I have code that parses it to JSON. 然后将回复解密,并且我有将其解析为JSON的代码。

InputStream myis = new ByteArrayInputStream(decryptedPayload);
new Avatar(myis);

decryptedPayload is a byte[]. decryptedPayload是一个byte []。

Some characters are foreign characters, for example, Russian or Chinese. 有些字符是外来字符,例如俄语或中文。 These just don't get sent properly through the GET parameters or when I'm receiving stuff and printing it out... 这些只是无法通过GET参数正确发送,或者当我收到东西并打印出来时...

For example here is a UTF-8 character ж And when I try and pass it through the GET parameter I get no proper response. 例如,这是一个UTF-8字符ж。当我尝试通过GET参数传递它时,我没有得到正确的响应。 http://185.112.249.77:9999/Api/search?search=ж As you can see the ж is getting changed to a ? http://185.112.249.77:9999/Api/search?search=ж正如您所看到的, ж正在变为? and then returning no results where as if that character was actually sent it should return results. 然后不返回任何结果,就好像该字符实际上已发送一样,它应该返回结果。

I am using Tomcat8. 我正在使用Tomcat8。 Any ideas on how I can fix this? 关于如何解决此问题的任何想法?

Thanks 谢谢

UPDATE 更新

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
     <title>Character encoding test page</title>
   </head>
   <body>
     <p>Data posted to this form was:
     <%
       request.setCharacterEncoding("UTF-8");
       out.print(request.getParameter("mydata"));
     %>

     </p>
     <form method="POST" action="test.jsp">
       <input type="text" name="mydata">
       <input type="submit" value="Submit" />
       <input type="reset" value="Reset" />
     </form>
   </body>
</html>

I did this on Localhost which has the same config as my production server by the way. 顺便说一下,我是在与我的生产服务器具有相同配置的Localhost上完成的。 http://localhost:8080/Api/test.jsp?mydata=жä Outputs "Data posted to this form was: жä" so it did work as it should... http:// localhost:8080 / Api / test.jsp?mydata =жä输出“发布到此表单的数据为:жä”,因此它确实可以正常工作...

Edit 2: 编辑2:

http://185.112.249.77:9999/Api/test?data=%D0%B6%C3%A4 http://185.112.249.77:9999/Api/test?data=%D0%B6%C3%A4

@WebServlet("/test")
public class test extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public test() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        response.getWriter().println(request.getParameter("data"));
    }

}

UPDATE 3 更新3

Ok in the servlet I changed response.getWriter().println(request.getParameter("data")); 在servlet中,我更改了response.getWriter().println(request.getParameter("data")); to response.getWriter().println(request.getParameter("data") +"\\n"+ "¢"); response.getWriter().println(request.getParameter("data") +"\\n"+ "¢"); and I now have this output: 现在我有这个输出:

жä жä

¢ ¢

Your request is defaulting to: 您的请求默认为:

Content-Type: application/json;charset=ISO-8859-1

Try setting all char encoding to UTF8 in Tomcat to test this. 尝试将所有char编码设置为Tomcat中的 UTF8进行测试。

And UTF-8 escape your request so: ж is %u0436 而且UTF-8逃脱了您的请求,所以:ж是%u0436

http://185.112.249.77:9999/Api/Search?search=%u0436 http://185.112.249.77:9999/Api/Search?search=%u0436

The above URL returns a lot of json information with this search parameter encoded. 上面的URL返回了很多json信息,并使用此搜索参数进行了编码。

You can use an online tool like this one to test. 您可以使用在线工具,像这样一个测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM