2012-01-09 1 views
5

두 개의 다른 출력 스트림을 포함하는 ZipInputStream을 javax.ws.rs.core.Response 스트림으로 반환하려고합니다. 스트림을 검색하기 위해 웹 서비스 호출을 할 때 빈 스트림을 돌려 받는다는 것을 알았습니다. 전에 GZipInputStream을 반환하려고 시도했으며 클라이언트 측에서 예상되는 스트림을 수신했습니다. ZipInputStream에 제대로 반환되지 않는 문제가있을 수 있습니까? 나는 2.4 (서블릿 API)를 모두 javax 사용하고 이 내 JAX-RS 서비스 (나는 조금을 단순화)처럼 보이는 방법입니다ZipInputStream을 Jax-RS 응답 객체로 반환

@GET 
@Produces({"application/zip", MediaType.APPLICATION_XML}) 
public Response getZipFiles(@PathParam("id") final Integer id){ 

    //Get required resources here 
    ByteArrayOutputStream bundledStream = new ByteArrayOutputStream(); 
    ZipOutputStream out = new ZipOutputStream(bundledStream); 
    out.putNextEntry(new ZipEntry("Item A")); 
    out.write(outputStream.toByteArray()); 
    out.closeEntry(); 

    out.putNextEntry(new ZipEntry("Item B")); 
    out.write(defectiveBillOutputStream.toByteArray()); 
    out.closeEntry(); 

    out.close(); 
    bundledStream.close(); 

    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bundledStream.toByteArray())); 
    return Response.ok(zis).build(); 
} 

을 그리고이 서비스를 호출하는 코드이다. 내가 축 1.4 사용하고 있습니다 : 마지막 줄에

HttpMethodBase getBillGroup = null; 
String id = "1234"; 
String absoluteUrl = baseURL + BASE_SERVICE_PATH.replace("@[email protected]",id) ; 
getZip = new GetMethod(absoluteUrl); 

HttpClient httpClient = new HttpClient(); 
try { 
     httpClient.executeMethod(getZip); 
} 
catch (Exception e) { 
     LOGGER.error("Error during retrieval " + e.getMessage()); 

} 

InputStream dataToConvert = getZip.getResponseBodyAsStream(); 
ZipInputStream in = new ZipInputStream(dataToConvert); 
ZipEntry itemA = in.getNextEntry(); 
//Do more things 

을 itemA은 첫 번째 항목은 JAX-RS 서비스의 스트림에 추가 된해야하지만, 내가 다시 널을 얻고있다. 무슨 일이 일어날 지 모르는 어떤 생각?

답변

1

첫 번째 블록에서는 ZipInputStream 대신 ByteArrayInputStream을 사용합니다.이 블록은 복잡한 zip 항목을 반복합니다.

+0

예! 고맙습니다. 이 작동합니다. 나는 어떤 데이터를 다시 얻고있다. (이전에는 모두 빈 스트림이었다.) 두 번째 블록에서 스트림을 변환하고 zipentries를 반복하는 방법을 알고 있습니까? 코드는 여전히 마지막 줄에 null을 반환합니다. – theseeker

+0

자, 이제 막했습니다 : ZipInputStream = new ZipInputStream (getZip.getResponseBodyAsStream()); 나는 itemA와 itemB를 얻습니다. 당신의 도움을 주셔서 감사합니다! – theseeker

+0

'@ Produces'는 MediaType.APPLICATION_XML 매개 변수 없이는 더 낫습니다. 그리고 스트림 대신 엔티티 매개 변수로 byte []를'ok '로 전달할 수 있습니다. _ 나는 경험이 없습니다. _ 당신은 먼저 바이너리 InputStream, i.o로 다운로드 할 수 있습니다. 시도 할 ZipInputStream. –