开发者

How to cast Representation to FileRepresentation in Restlet

I am using Restlet framework. I create one web service that returns a file to the client.

On the server side, I first create a FileRepresentation object, instantiate it correctly, and return it to the client as Represent开发者_开发百科ation.

On the client side, I want to extract the content of the Representation, how can I cast the Representation object to FileRepresentation?

Thanks in advance!!


In fact, the FileRepresentation class is provided in order to fill request / response from a file but can't be used to extract content of a response.

To have access to your response content on the client side, it depends on the file type. If you receive an ascii content, you can do something like that:

Representation representation = resource.get();
String fileContent = representation.getText();

If it's a binary file, you need to work with a stream, as described below:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
resource.get().write(outputStream);
byte[] fileContent = outputStream.toByteArray();

Hope it helps you, Thierry

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜