2013-08-20 77 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