Return File From Resteasy Server

一世执手 提交于 2019-12-21 04:13:38

问题


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.


回答1:


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();
}


来源:https://stackoverflow.com/questions/8147956/return-file-from-resteasy-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!