1

according to this tutorial Base64 download REST web service I am trying to download from my client app, multiple images stored in a byte array who are send from my REST service. The REST resource file contains:

//****RESTful Service****
@GET    // HTTP GET request
@Path("get_pictures/{userid}")
@Produces( MediaType.APPLICATION_OCTET_STREAM )
// Gets all the pictures for the report
public Response getReportPictures(@PathParam("userid") int userid) {
    return wsmanager.getReportPictures(userid);
}

In the below getReportPicture() method I am serializing the images and store them in an ArrayList which I want to return via the above REST service. The Image class is one of my libs to serialize and unserialize pictures. The serializing process and returning the ArraList of byte[] works well.

    //**** getReportPicture() method *****
    ArrayList<byte[]> pictures = new ArrayList<byte[]>();
    ...
    Response response = null;
    ...
    byte[] img = Image.serialize(strPictureFile);       
    pictures.add(img);
    ... 
    ResponseBuilder builder = Response
        .ok( pictures, "image/jpg" )
        .header( "Content-Disposition","inline; filename = \"" + name + "\"" );
        response = builder.build();

    return response;

At my Java client I am trying to read the content from the REST service, which is not working implementing the below code:

    WebTarget target = baseTarget.path("get_pictures/"+userid);
    Response clResponse = target
            .request(MediaType.APPLICATION_OCTET_STREAM)
            .get();
    String s = clResponse.readEntity(String.class);

Can someone help please?.

Mike
  • 37
  • 2
  • 7
  • When you say "which is not working", can you elaborate what exactly is not working? – onnoweb May 18 '18 at 18:13
  • It would probably be easier to just return a list of urls for the images. and then just load the urls from the client when it needs to display the images. saving on network usage. some server code for returning images https://stackoverflow.com/questions/40557637/how-to-return-an-image-in-spring-boot-controller-and-serve-like-a-file-system/44813266#44813266 – mavriksc May 18 '18 at 19:52
  • "which is not working" means that the REST service sends the data as it should, but I can not extract them from the response message. Unfortunately, I need the images and not the url for my project. – Mike May 18 '18 at 20:04
  • what would stop you from using a list of urls to fetch individual images? also your response type is not correct. If you were to send one image it would be image/jpg but a java `List` of images is not something a browser or http client are going to be able to handle with out some unpacking. – mavriksc May 18 '18 at 21:03
  • after looking at the client code.... it should be like `List imgs = clResponse.readEntity(List.class);` instead of string. because that's the type, what you are doing is trying to convert List to String – mavriksc May 18 '18 at 21:08

0 Answers0