繁体   English   中英

球衣休息和csv响应

[英]jersey rest and csv response

我创建了一个休息电话,该电话使用泽西岛(Jersey)响应CSV文件。

其余呼叫代码为:

@GET
@Path("/ReportWithoutADEStatus")
@Produces({ "application/ms-excel"})
public Response generateQurterlyReport(){
    QuarterlyLabelReport quartLabelReport = new QuarterlyLabelReport();
    String fileLoc=quartLabelReport.generateQurterlyLblRep(false);
    File file=new File(fileLoc);
    return Response.ok(fileLoc,"application/ms-excel")
            .header( "Content-Disposition","attachment;filename=QuarterlyReport_withoutADE.csv")
            .build();
}

上面的代码读取在临时位置创建的csv文件,并使用rest调用响应该csv。 这是完美的工作。 但是现在要求已更改。 将文件内容流式传输到内存中,然后从Rest API以csv格式进行响应。
我从未做过流式传输到内存中的文件并在REST中响应内容的事情。

有人可以帮我吗?

提前致谢。

您需要使用StreamingResponse作为响应实体。 在我的项目中,我做了一个简单的方法来从字节数组中返回这些值。 您只需要先将文件准备成一个字节,然后调用此命令:

private StreamingOutput getOut(final byte[] excelBytes) {
    return new StreamingOutput() {
        @Override
        public void write(OutputStream out) throws IOException, WebApplicationException {
            out.write(excelBytes);
        }
    };
}

然后在您的主要方法中,您将像:

return Response.ok(getOut(byteArray)).build(); //add content-disp stuff here too if wanted

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM