Wrong Charset Encoding with Play Framework 2.1

2019-07-18 03:16发布

I have a web service that receives a parameter in ISO-8859-1 encoding.

But when I try to read it from the request, I get this characters:

�����

I've tryied all these approaches, but none of the convert the given string to the expected one (áéíóú):

    val a = new String(_html.getBytes());
    val b = new String(_html.getBytes(), "UTF-8")
    val c = new String(_html.getBytes(), "ISO-8859-1")
    val d = new String(_html.getBytes("ISO-8859-1"), "UTF-8")
    val e = new String(_html.getBytes("ISO-8859-1"), "ISO-8859-1")
    val f = new String(_html.getBytes("UTF-8"), "UTF-8")
    val g = new String(_html.getBytes("UTF-8"), "ISO-8859-1")

Here is my action:

  val inboundMessageForm = Form(
    mapping(
      "html" -> text)(InboundMessage.apply)(InboundMessage.unapply))

  def forward = Action(parse.multipartFormData) { implicit request =>
    val inboundMessage = inboundMessageForm.bindFromRequest.get

        // inboundMessage.html =>  �����
   }

What can I do to solve this problem?

2条回答
男人必须洒脱
2楼-- · 2019-07-18 03:58

If you are running windows, the default encoding (at least in the states, not sure about the rest of the world) is actually "windows-1252", not "ISO-8859-1". You could also try "UTF-16" in case those characters aren't included in the "UTF-8" encoding (which most things other than windows use).

查看更多
登录 后发表回答