小编the*_*ker的帖子

将ZipInputStream作为Jax-RS Response对象返回

我试图返回一个包含两个不同输出流的ZipInputStream作为javax.ws.rs.core.Response Stream.当我进行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();
 }
Run Code Online (Sandbox Code Playgroud)

这是调用服务的代码.我使用轴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) { …
Run Code Online (Sandbox Code Playgroud)

java rest jax-rs zipinputstream

5
推荐指数
1
解决办法
4100
查看次数

标签 统计

java ×1

jax-rs ×1

rest ×1

zipinputstream ×1