球衣休息和csv反应

ram*_*ram 9 java rest jersey-1.0 jersey-2.0

我创建了一个休息调用,使用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();
}
Run Code Online (Sandbox Code Playgroud)

上面的代码读取在临时位置创建的csv文件,并使用rest调用响应csv.这完全正常.但现在要求已经改变了.在内存中传输文件内容,并以Rest API中的csv格式对其进行响应.
我从来没有完成流式传输到内存中的文件并回复REST中的内容.

有人可以帮我这个吗?

提前致谢.

nic*_*art 15

您需要使用StreamingResponse作为响应实体.在我的项目中,我做了一个简单的方法从字节数组中返回这些.你必须首先将文件准备好为一个字节,然后调用:

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

然后在你的主要方法中,你会喜欢:

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