0
我目前使用的是Jersey 2.13。我希望客户端从服务器下载zip文件,并且我希望服务器在下载完成后删除文件。因此,我尝试了AsyncResponse,所以我可以注册一个CompletionCallback来知道下载完成。对于我当前的代码,客户端得到:“失败:HTTP错误代码:415”。Jersey压缩文件的异步下载
服务器代码:
@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.9](https://jersey.java.net/nonav/documentation/1.9/user-guide.html) – 2014-11-04 13:43:06
你是对的,显然我使用泽西岛2.13 – 2014-11-04 13:54:58
但是您使用的旧版客户端API在2.13中不可用。这怎么可能?他们是两个不同的应用程序无论如何,我无法重现这个问题,尽管我使用的是新的客户端API。不知道这是否有所作为。 – 2014-11-04 14:00:55