泽西岛异步下载一个zip文件

时间:2014-11-04 09:56:15

标签: java asynchronous jersey jax-rs

我目前正在使用Jersey 2.13。我希望客户端从服务器下载一个zip文件,我希望服务器在下载完成后删除该文件。因此,我尝试了AsyncResponse,因此我可以注册CompletionCallback以了解下载已完成。对于我当前的代码,客户端得到:"失败:HTTP错误代码:415"。

服务器代码是:

@GET
@Path("/{id}")
@Produces({"application/zip"})
public void getResourceTree(@Suspended final AsyncResponse asyncResponse, @PathParam("id") String id) throws IOException{
    asyncResponse.register(new CompletionCallback() {
        @Override
        public void onComplete(Throwable throwable) {

            if (throwable == null) {
                // no throwable - the processing ended successfully
                // (response already written to the client)
                // Delete temporary zip file...
            } else {
                throw new UnexpectedException("The user did not receive the zip file in path:"+ getResourceTreeZipName(id) +" successfully");
            }
        }
    });

    new Thread(new Runnable() {
        @Override
        public void run() {
                File file = createZipFile(id);
                asyncResponse.resume(Response.ok((Object) file).build());
        }
    }).start();
}

客户端代码是:

private File downloadZipFile(String resourceId){
    Client client = Client.create();
    WebResource serverWebResource = client.resource(serverURI+"/"+resourceId);
    ClientResponse response = serverWebResource.accept("application/zip").get(ClientResponse.class);
    InputStream in = response.getEntityInputStream();
    //Create and return the zip file from the input stream...
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您是否尝试使用其他REST工具,例如Postman,并检查服务器是否正在返回正确的对象,当GET使用accept标头调用它时?我要做的第二件事是调试客户端并检查是否在创建对象后立即设置了header accept。