How to send image to Restful Jax-rs

I want to send an image from a java server (Restful Jax-rs). My client is Android.

@GET
public Response getUserImage() {
byte[] image =new byte[1024];
return Response.ok(image, MediaType.APPLICATION_OCTET_STREAM).header("content-attachment; filename=image_from_server.png") .build();

But here comes one download. So I want to download without loading, when I run the request URL in the browser, it should open automatically. Thank.

+3
source share
2 answers

I believe that as you indicated application/octet-stream.

I think you should use image/jpegor image/png.

@GET
@Produces({"image/png"})
public Response getUserImage() {

    final byte[] image = ...;
    // say your image is png?

    return Response.ok().entity(new StreamingOutput(){
        @Override
        public void write(OutputStream output)
           throws IOException, WebApplicationException {
           output.write(image);
           output.flush();
        }
    }).build();
}
+8
source

base 64, base 64 xml json . , . , 30%. Restle rest api, , JAX-RS.

0

All Articles