GWT HTTP 响应 getText() 作为二进制
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.您可以使用 JSNI 在 GWT 中获取二进制图像。请注意,它不适用于 IE。
这是一个示例:
我刚刚完成了一个类似问题的研究,我在其中添加了附加信息:
使用 java gwt 生成内联图像
You can get binary image in GWT using JSNI. Mind that it doesn't work with IE.
This is an example how:
I just finished researching a similar question where I put additional information:
Generating an inline-image with java gwt