Below is my code to download a file from remote server.
Based on the URL, either a text file or tar.gz file will be downloaded. I am trying to make an abstract implementation to download any type of file.
However, the below code is working fine for the txt file; but tar.gz are corrupted.
httpClient.get(downloadUrl, new Handler<HttpClientResponse>() {
public void handle(final HttpClientResponse httpEvent) {
//pause the http response till we complete setting up our
//async file handler
httpEvent.pause();
//setup file open handler
vertx.fileSystem().open(target, null, false, true, true, true, new Handler<AsyncResult<AsyncFile>>() {
public void handle(AsyncResult<AsyncFile> fileEvent) {
if(fileEvent.failed()){
fileEvent.cause().printStackTrace();
return;
}
final AsyncFile asynFile = fileEvent.result();
final Pump downloadPump = Pump.createPump(httpEvent, asynFile);
downloadPump.start();
httpEvent.endHandler(new Handler<Void>() {
public void handle(Void event) {
//close the file
asynFile.flush().close(new Handler<AsyncResult<Void>>() {
public void handle(AsyncResult<Void> event) {
System.out.println("Is file close ok? = " + event.failed());
}
});
System.out.println("File download operation complets: Download size = "
+ downloadPump.bytesPumped());
}
});
}
});
//resume the receive operation
httpEvent.resume();
}
}).exceptionHandler(new Handler<Throwable>() {
public void handle(Throwable event) {
System.out.println(event);
}
}).end();