2

In my server side i have an image that will be sended via rest to the client side After sending, the image should be deleted.

So the image will be coppied on a fileoutputstream and i send the fileoutputstream to the client side and i delete the image.

i use the following code:

@GET
@Path("/get_image")
@Produces({ MediaType.APPLICATION_OCTET_STREAM })    
public Response getImage(){

.......

    File image = new File("myimage.png");
    FileOutputStream oos = new FileOutputStream("imageToSend.png");
    byte[] buf = new byte[8192];
            InputStream is = new FileInputStream(image);
            int c = 0;
            while ((c = is.read(buf, 0, buf.length)) > 0) {
                oos.write(buf, 0, c);
                oos.flush();
            }

            ResponseBuilder response = Response.ok(oos);
            response.header("Content-Disposition",
                    "attachment; filename=image.png");
            oos.close();
            is.close();

            image.delete();

            return response.build();
        }
    }

but when i execute the getImage method i found this error

org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: java.io.FileOutputStream of media type: application/octet-stream
    at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:67)
    at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:411)

If any one have idea about the cause of this problem....

kind regards

Yahia Ammar
  • 280
  • 4
  • 20
  • What do you expect Resteasy to do with an output stream? It can't read from it, and there's no interface to get the file which the stream represents. – Kenster Jul 03 '15 at 22:49
  • @Kenster, i would like to delete an image after it is sent: `File image = new File(pathImage); ResponseBuilder response = Response.ok((Object)image); response.header("Content-Disposition", "attachment; filename=tsunami_image.png"); image.delete() return response.build();` if i set the image object in the response and i delete the image, i got **fileNotFoundExeption** error. then according to my research on the net, they advise to write directly the output stream in response and then you can delete the file. But I have not been able to make this .... – Yahia Ammar Jul 06 '15 at 14:01
  • Maybe you could share some of this research you found about passing output streams to resteasy. – Kenster Jul 06 '15 at 14:09
  • @Kenster for exemple in this topic http://stackoverflow.com/questions/26930127/how-to-delete-file-after-rest-response, they advise to use output stream – – Yahia Ammar Jul 06 '15 at 15:08

1 Answers1

2

You should either just use the InputStream as the response body (RESTeasy will do the writing for you), or you can use StreamingOutput to write the data straight to the response stream. For example

StreamingOutput output = new StreamingOutput() {
    @Override
    public void write(OutputStream out) {
        InputStream is = new FileInputStream(image);
        int c = 0;
        while ((c = is.read(buf, 0, buf.length)) > 0) {
            oos.write(buf, 0, c);
            oos.flush();
        }
        is.close();
    }
}

ResponseBuilder response = Response.ok(output);

It may just be easier to use return the InputStream instead. It pretty much does the same thing under the hood, as using the StreamingOutput like above. One odd thing I've faced in the past is with large files, when trying to return InputStream. With StreamingOutput, it seemed to work better.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720