简体   繁体   English

球衣休息和csv响应

[英]jersey rest and csv response

I have created a rest call that responds back with the CSV file using Jersey. 我创建了一个休息电话,该电话使用泽西岛(Jersey)响应CSV文件。

rest call code is: 其余呼叫代码为:

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

The above code reads a csv file created in a temp location and responds that csv using rest call. 上面的代码读取在临时位置创建的csv文件,并使用rest调用响应该csv。 This is perfectly working fine. 这是完美的工作。 But now the requirement has changed. 但是现在要求已更改。 stream the file content in memory and respond that in csv format from Rest API. 将文件内容流式传输到内存中,然后从Rest API以csv格式进行响应。
I have never done streaming to a file in memory and responding back the content in REST. 我从未做过流式传输到内存中的文件并在REST中响应内容的事情。

Can somebody help me with this? 有人可以帮我吗?

Thanks in advance. 提前致谢。

You need to use a StreamingResponse as your response entity. 您需要使用StreamingResponse作为响应实体。 In my projects I've made a simple method to return on of these from a byte array. 在我的项目中,我做了一个简单的方法来从字节数组中返回这些值。 You just have to ready the file into a byte are first, then call this: 您只需要先将文件准备成一个字节,然后调用此命令:

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

Then in your main method you would something like: 然后在您的主要方法中,您将像:

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