5

我正在尝试将包含两个不同输出流的 ZipInputStream 作为 javax.ws.rs.core.Response 流返回。当我进行 Web 服务调用以检索流时,我注意到我得到了一个空流。我之前尝试过返回 GZipInputStream,并且在客户端收到了预期的流。ZipInputStream 是否存在阻止其正确返回的问题?我正在使用 javax 2.4 (servlet-api) 这就是我的 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("@id@",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 服务中添加到流中的第一个条目,但我得到了一个空值。知道可能是什么原因造成的吗?

4

1 回答 1

1

在第一个块中使用 aByteArrayInputStream而不是 a ZipInputStream,它会迭代复杂的 zip 条目。

于 2012-01-09T20:00:35.533 回答