GWT HTTP 响应 getText() 作为二进制

发布于 2024-11-07 14:48:05 字数 920 浏览 0 评论 0原文

我正在开发一个 GWT 应用程序,它对二进制数据进行 REST 调用。我正在尝试使用 GWT 的 RequestBuilder。问题是响应仅提供 getText() 方法。

这是重现该问题的最简单的示例:

private static void sendRequest()
{
    String url = URL.encode("/object/object_id");

    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
    try
    {
        requestBuilder.sendRequest("", new RequestCallback()
        {
            @Override
            public void onResponseReceived(Request request, Response response)
            {
                String data = response.getText(); ///< Need this to be a byte[] array (e.g. getData())
            }

            @Override
            public void onError(Request request, Throwable exception)
            {
            }
        });
    }
    catch (RequestException RequestException)
    {
    }
}

问题是 GWT 将响应数据编码为字符串(我认为)是默认平台的编码。在 GWT 将数据转换为字符串之前,有什么方法可以获取数据吗?

I'm working on a GWT App that makes a REST call for binary data. I'm trying to use GWT's RequestBuilder. The problem is that the response only offers a getText() method.

Here's the simplest example that reproduces the problem:

private static void sendRequest()
{
    String url = URL.encode("/object/object_id");

    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
    try
    {
        requestBuilder.sendRequest("", new RequestCallback()
        {
            @Override
            public void onResponseReceived(Request request, Response response)
            {
                String data = response.getText(); ///< Need this to be a byte[] array (e.g. getData())
            }

            @Override
            public void onError(Request request, Throwable exception)
            {
            }
        });
    }
    catch (RequestException RequestException)
    {
    }
}

The problem is that GWT is encoding the response data as a String in (what I think) is the default platform's encoding. Is there any way to get the data before GWT converts it to a String?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

标点 2024-11-14 14:48:05

HTTP可以传输文本和二进制,但Javascript只能通过XHR获取文本。如果您想通过它发送二进制数据,请对其进行 Base64 编码。 GWT 可以处理 Base64

更新:在最新的浏览器(2013 年底)中,可以通过 TypedArray。请参阅浏览器支持

HTTP can transfer text and binary, but Javascript can only get text via XHR. If you want to send binary data through it then Base64 encode it. GWT can handle Base64.

Update: in recent browsers (end of 2013), the binary array handling can be achieved via TypedArray. See browser support for it.

街角卖回忆 2024-11-14 14:48:05

您可以使用 JSNI 在 GWT 中获取二进制图像。请注意,它不适用于 IE。
这是一个示例:

native String getBinaryResource(String url) /*-{
    // ...implemented with JavaScript                 
    var req = new XMLHttpRequest();
    req.open("GET", url, false);  // The last parameter determines whether the request is asynchronous -> this case is sync.
    req.overrideMimeType('text/plain; charset=x-user-defined');
    req.send(null);
    if (req.status == 200) {                    
        return req.responseText;
    } else return null
}-*/;

我刚刚完成了一个类似问题的研究,我在其中添加了附加信息:
使用 java gwt 生成内联图像

You can get binary image in GWT using JSNI. Mind that it doesn't work with IE.
This is an example how:

native String getBinaryResource(String url) /*-{
    // ...implemented with JavaScript                 
    var req = new XMLHttpRequest();
    req.open("GET", url, false);  // The last parameter determines whether the request is asynchronous -> this case is sync.
    req.overrideMimeType('text/plain; charset=x-user-defined');
    req.send(null);
    if (req.status == 200) {                    
        return req.responseText;
    } else return null
}-*/;

I just finished researching a similar question where I put additional information:
Generating an inline-image with java gwt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文