2013-08-20 75 views
9

我创建了一个休息通话,使用泽西岛的CSV文件作出回应。泽西岛休息和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文件,并响应使用其余呼叫CSV。这是完美的工作正常。但现在这个要求已经改变了。 将内容中的文件内容进行流式处理,并以Rest API的csv格式进行响应。
我从来没有对内存中的文件进行流式处理,并对REST中的内容进行响应。

有人可以帮助我吗?

在此先感谢。

回答

15

您需要使用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 
+0

它的工作,你只需要准备好文件转换成字节是第一,然后调用它。感谢您的解决方案。 – ram

+0

如果解决了问题,您是否可以将答案标记为已接受?谢谢! –

+0

什么冲洗? – Dejell