[英]Jersey asynchronous download of a zip file
我當前正在使用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...
}
我究竟做錯了什么?
您是否嘗試過使用其他REST工具(例如Postman),並在GET使用accept標頭調用服務器時檢查服務器是否返回了正確的對象? 我要做的第二件事是調試客戶端,並檢查對象創建后是否立即設置標頭接受。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.