2013-08-20 2 views
9

Jersey를 사용하여 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를 응답합니다. 이것은 완벽하게 잘 작동합니다. 그러나 이제 요구 사항이 변경되었습니다. 파일 내용을 메모리에 스트리밍하고 나머지 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