Skip to content

Instantly share code, notes, and snippets.

@xhanin
Created August 13, 2012 19:48
Show Gist options
  • Save xhanin/3343588 to your computer and use it in GitHub Desktop.
Save xhanin/3343588 to your computer and use it in GitHub Desktop.
Download file with Vert.x (not working with vert.x 1.2.3)
import static org.vertx.groovy.core.streams.Pump.createPump
vertx.createHttpClient(host: 'humanstxt.org', port: 80)
.getNow('/humans.txt') { resp ->
println "GOT response with status $resp.statusCode"
resp.pause();
resp.endHandler {
println "response END with first registered handler"
}
def filename = "${UUID.randomUUID()}.txt"
vertx.fileSystem.open(filename) { ares ->
println "file opened"
def file = ares.result
def pump = createPump(resp, file.writeStream)
resp.endHandler {
println "response END with handler after file opening"
file.close {
println "Stored ${pump.bytesPumped} bytes to $filename"
}
}
pump.start()
resp.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment