gpt4 book ai didi

java - Jersey 休息和 csv 响应

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:12:00 25 4
gpt4 key购买 nike

我创建了一个 rest 调用,它使用 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);
}
};
}

然后在你的 main 方法中你会是这样的:

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

关于java - Jersey 休息和 csv 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18342895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com