Search code examples
androidfilerestlet

Getting file form Restlet Response


I created webapp which sends file by FileRepresentation. Client is an Android app. How can I get File from Restlet Response object on the client side?


Solution

  • The file content will be present within the payload. So you can extract it like any payload with Restlet, as described below:

    ClientResource cr = new ClientResource(...);
    Representation rep = cr.get();
    

    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 = cr.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();
    cr.get().write(outputStream);
    byte[] fileContent = outputStream.toByteArray();
    

    Hope it helps you, Thierry