java - 从 Resteasy 服务器返回文件

标签 java rest file-io resteasy java-io

你好,我想从 resteasy 服务器返回一个文件。为此,我在客户端有一个链接,它使用 ajax 调用休息服务。我想在休息服务中返回文件。我尝试了这两个代码块,但都没有按我希望的那样工作。

    @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;
}

当我从 firebug 控制台检查时,这两个代码块都写了“Sedat BaSAR”以响应 ajax 调用。但是,我想将“Sedat BaSAR”作为文件返回。我该怎么做?

提前致谢。

最佳答案

有两种方法。

1st - 返回一个 StreamingOutput 实例。

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

可以返回文件大小加上Content-Length头,如下例:

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

但如果您不想返回 StreamingOutput 实例,还有其他选择。

第二 - 将输入流定义为实体响应。

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

    return Response.code(200).entity(is).build();
}

关于java - 从 Resteasy 服务器返回文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8147956/

相关文章:

java - 将 Flux 条目与 previous 和 map 相结合

java - @PreAuthorize 注解无法解析变量

web-services - 机器可读格式的 REST 服务示例

java - 如何使用java从文本文件中删除一行?

java - 如何将文件写入 Android SD 卡并在 PC 上可见

java - 空列表等于空列表

java - while 循环内跳过扫描仪输入

java - 使用 HTTP Get 发送数组

spring - 使用 Maven 和 tomcat 的 CXF Rest

java - 黑莓读取项目中的本地属性文件