Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return File From Resteasy Server

Hi, I wanted to return a file from a resteasy server. For this purpose, I have a link at the client side which is calling a rest service with ajax. I want to return the file in the rest service. I tried these two blocks of code, but both didn't work as I wanted them to.

    @POST
    @Path("/exportContacts")
    public Response exportContacts(@Context HttpServletRequest request, @QueryParam("alt") String alt) throws  IOException {

            String sb = "Sedat BaSAR";
            byte[] outputByte = sb.getBytes();


    return Response
            .ok(outputByte, MediaType.APPLICATION_OCTET_STREAM)
            .header("content-disposition","attachment; filename = temp.csv")
            .build();
    }

.

@POST
@Path("/exportContacts")
public Response exportContacts(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("alt") String alt) throws IOException {

    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=temp.csv");
    ServletOutputStream out = response.getOutputStream();
    try {

        StringBuilder sb = new StringBuilder("Sedat BaSAR");

        InputStream in =
                new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
        byte[] outputByte = sb.getBytes();
        //copy binary contect to output stream
        while (in.read(outputByte, 0, 4096) != -1) {
            out.write(outputByte, 0, 4096);
        }
        in.close();
        out.flush();
        out.close();

    } catch (Exception e) {
    }

    return null;
}

When I checked from the firebug console, both of these blocks of code wrote "Sedat BaSAR" in response to the ajax call. However, I want to return "Sedat BaSAR" as a file. How can I do that?

Thanks in advance.

like image 654
Sedat Başar Avatar asked Nov 16 '11 07:11

Sedat Başar


1 Answers

There're two ways to to it.

1st - return a StreamingOutput instace.

@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response download() {
    InputStream is = getYourInputStream();

    StreamingOutput stream = new StreamingOutput() {

        public void write(OutputStream output) throws IOException, WebApplicationException {
            try {
                output.write(IOUtils.toByteArray(is));
            }
            catch (Exception e) {
                throw new WebApplicationException(e);
            }
        }
 };

 return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition", "attachment; filename=\"temp.csv\"").build();
}

You can return the filesize adding Content-Length header, as the following example:

return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition", "attachment; filename=\"temp.csv\"").header("Content-Length", getFileSize()).build();

But if you don't want to return a StreamingOutput instance, there's other option.

2nd - Define the inputstream as an entity response.

@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response download() {
    InputStream is = getYourInputStream();

    return Response.code(200).entity(is).build();
}
like image 124
PeDiVo Avatar answered Oct 17 '22 01:10

PeDiVo