为什么我的 Unicode 字符串在从 Java Applet 传递到 Java Script 时会损坏? [英] Why does my Unicode String get corrupted, when passed from Java Applet to Java Script?

查看:13
本文介绍了为什么我的 Unicode 字符串在从 Java Applet 传递到 Java Script 时会损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,所以不要太苛刻:)

I'm pretty new, so don't be too harsh :)

我在将 unicode String 从网页中嵌入的 javax.swing.JApplet 传递到 Java Script 部分时遇到问题.我不确定这是错误还是对相关技术的误解:

I'm facing a problem passing an unicode String from an embedded javax.swing.JApplet in a web page to the Java Script part. I'm not sure this is whether a bug or a misunderstanding of the involved technologies:

我想将一个 unicode 字符串从 Java Applet 传递到 Java Script,但是字符串被弄乱了.奇怪的是,该问题不会出现在 Internet Explorer 10 中,而是出现在 Chrome (v26)Firefox (v20) 中.不过我还没有测试过其他浏览器.

I want to pass a unicode string from a Java Applet to Java Script, but the String gets messed up. Strangely, the problem doesn't occur not in Internet Explorer 10 but in Chrome (v26) and Firefox (v20). I haven't tested other browsers though.

返回的字符串似乎没问题,除了最后一个 unicode 字符.Java Script Debugger 和 Web Page 中的结果将是:

The returned String seems to be okay, except for the last unicode character. The result in the Java Script Debugger and Web Page would be:

  • abc → abc
  • 表示→表
  • ま → ま
  • ウォッチリスト → ウォッチリス
  • アップロード → アップロー
  • ホ →
  • ホ → ホ(不确定)
  • アップロードabc → アップロードabc

该字符串似乎在最后一个字节处损坏了.如果它以 ASCII 字符结尾,则字符串没问题.此外,问题不会出现在每个组合中,也不是每次都出现(对此不确定).因此,我怀疑存在错误,我担心我可能会发布无效问题.

The string seems to get corrupted at the last bytes. If it ends with an ASCII character the string is okay. Additionally the problem doesn't occur within every combination and also not every time (not sure on this). Therefore I suspect a bug and I'm afraid I might be posting an invalid question.

一个简约的设置包括一个返回一些 unicode (UTF-8) 字符串的小程序:

A minimalistic set up includes an applet that returns some unicode (UTF-8) strings:

/* TestApplet.java */
import javax.swing.*;

public class TestApplet extends JApplet {

private String[] testStrings = {
            "abc", // OK (because ASCII only)
            "表示", // Error on last Character
            "表示", // Error on last Character
            "ホーム ", // OK (because of *space* after ム)
            "アップロード", ... }; 
    public TestApplet() {...};     // Applet specific stuff

    ...

    public int getLength() { return testStrings.length;};

    String getTestString(int i) {
        return testStrings[i];    // Build-in array functionality because of IE. 
    }
}

带有java脚本的相应网页可能如下所示:

The corresponding web page with java script could look like this:

 /* test.html */
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <span id="output"/>
        <applet id='output' archive='test.jar' code=testApplet/>
    </body>

    <script type="text/javascript" charset="utf-8">
        var applet = document.getElementById('output');
        var node = document.getElementById("1");
        for(var i = 0; i < applet.getLength(); i++) {
             var text = applet.getTestString(i);
         var paragraphNode = document.createElement("p");
         paragraphNode.innerHTML = text;
         node.appendChild(paragraphNode);
        }
    </script>
</html>

环境

我正在使用适用于 Mozilla 浏览器的下一代 Java 插件 10.21.2"在当前 Java 版本 1.7.0_21 的 Windows 7 32 位上工作.我的操作系统区域设置存在一些问题,但我尝试了多种(英语、日语、中文)区域设置.

Environment

I'm working on Windows 7 32-Bit with the current Java Version 1.7.0_21 using the "Next Generation Java Plug-in 10.21.2 for Mozilla browsers". I had some problems with my operating system locale, but I tried several (English, Japanese, Chinese) regional settings.

如果字符串损坏,chrome 会显示无效字符(例如 ).另一方面,Firefox 会完全删除字符串,如果它以 结尾.

In case of an corrupt String chrome shows invalid characters (e.g. ��). Firefox, on the other hand, drops the string completly, if it would be ending with ��.

Internet Explorer 设法正确显示字符串.

Internet explorer manages to display the strings correctly.

我可以想象几种解决方法,包括转义/取消转义和添加一个最终字符",然后通过 java 脚本删除它.实际上,我打算针对 Android 的 Webkit 编写代码,但我还没有在那里对其进行测试.

I can imagine several workarounds, including escaping/unescaping and adding a "final char" which then is removed via java script. Actually I'm planning to write against Android's Webkit, and I haven't tested it there.

由于我想继续在 Chrome 中进行测试,(因为 Webkit 技术和舒适性)我希望有一个简单的解决方案来解决这个问题,但我可能忽略了这一点.

Since I would like to continue testing in Chrome, (because of Webkit technology and comfort) I hope there is a trivial solution to the problem, which I might have overlooked.

推荐答案

您需要确保将以下 Java Argument 添加到您的小程序/嵌入标签中:

You need to make sure to add the following Java Argument to your applet/embed tag:

-Dfile.encoding=utf-8

-Dfile.encoding=utf-8

即java_arguments="-Dfile.encoding=utf-8"

i.e. java_arguments="-Dfile.encoding=utf-8"

否则,它会将小程序视为 ASCII 文本.

Otherwise it is going to expect and treat the applet as ASCII text.

这篇关于为什么我的 Unicode 字符串在从 Java Applet 传递到 Java Script 时会损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆