1

我显然在这里遗漏了一些东西。我有一个网络应用程序,其中表单的输入可能是英语,或者在键盘切换后是俄语。页面的元标记指定页面为 UTF-8。这似乎无关紧要。

如果我输入“вв”,两个 unicode 字符:西里尔小写字母 VE

我能得到什么?一个字符串。我调用 getCodePoints().toArray() 并得到:

 [208, 178, 208, 178]

如果我调用 chars().toArray[],我会得到相同的结果。

有没有搞错?

我完全可以控制网页,但当然会有不同的浏览器。但是我怎样才能从网页上得到一些东西,让我得到正确的西里尔字符呢?

这是在 java 1.8.0_312 上。我可以升级一些,但不能一直升级到最新的 java。

页面是这样的:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
 <html>
   <head>
     <title>Cards</title>
     <link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity = "sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin = "anonymous" />
     <link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity = "sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin = "anonymous" />
     <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity = "sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin = "anonymous">
     </script>
     <meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" />
     <style>.table-nonfluid { width: auto !important; }</style>
   </head>
   <body>
     <div style = "padding: 25px 25px 25px 25px;">
       <h2 align = "center">Cards</h2>
       <div style = "white-space: nowrap;">
         <a href="/cgi-bin/WebObjects/app.woa/wo/ee67KCNaHEiW1WdpdA8JIM/2.1">Home</a>
         <div>
   <form name="f_3_1" method="post" action="/cgi-bin/WebObjects/app.woa/wo/ee67KCNaHEiW1WdpdA8JIM/2.3.1">
     <table class = "table" border = "1" style = "max-width: 50%; font-size: 300%; text-align: center;">
           <tr>
             <td>to go</td>
           </tr>
           <tr>
             <td><input size="25" type="text" name="3.1.5.3.3" /></td>
           </tr>
           <td>
             <input type="submit" value="Submit" name="3.1.5.3.5" />&nbsp;&nbsp;<a href="/cgi-bin/WebObjects/app.woa/wo/ee67KCNaHEiW1WdpdA8JIM/2.3.1.5.3.7">Skip</a>
           </td>
     </table>
   <input type="hidden" name="wosid" value="ee67KCNaHEiW1WdpdA8JIM" />
 </form>
 </div>
       </div>
     </div>
   </body>
 </html>

嗯。好吧,这至少是故事的一部分。

我有这个代码:

    System.out.println("start: " + start);
    int[] points = start.chars().toArray();
    byte[] next = new byte[points.length];
    int idx = 0;
    System.out.print("fixed: ");
    for (int p : points) {
        next[idx] = (byte)(p & 0xff);
        System.out.print(Integer.toHexString(next[idx]) + " ");
        idx++;
    }
    System.out.println("");

输出是:

 start: вв
 fixed: ffffffd0 ffffffb2 ffffffd0 ffffffb2 

“В”的 UTF-8 值(以十六进制表示)是 d0b2。

所以,就是这样。问题是,为什么这不容易获得?我真的必须逐个字节对地把它放在一起吗?

如果字符串已经是 UTF-8,我想我们可以看到它,为什么 codePoints() 方法不给我们,你知道,codePoints?

好的,所以现在我这样做:

 new String(next, StandardCharsets.UTF_8);

我得到了正确的字符串。但是 codePoints() 给我一个 IntStream 似乎仍然很奇怪,但是如果你将这些东西用作 int 值,它就会被破坏。

4

1 回答 1

0

这是我使用的框架的问题。我以为我将请求和响应内容类型设置为 utf-8 但我没有。

于 2022-01-24T01:47:16.880 回答