使用Vert.x方便的进行文件的异步下载,为了更加方便,这里使用groovy。(java同理)
import io.vertx.core.Vertx
import io.vertx.ext.web.Router
def vertx = Vertx.vertx()
def server = vertx.createHttpServer()
def router = Router.router(vertx)
router.get("/api/file").handler(rc -> {
rc.response().sendFile("your-path/your-filename")
})
server.requestHandler(router).listen(8080, ar -> {
if (ar.succeeded()) {
println("start successful")
}
})
import io.vertx.core.Vertx
import io.vertx.core.file.OpenOptions
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.client.WebClient
import io.vertx.ext.web.codec.BodyCodec
Vertx vertx = Vertx.vertx()
def webClient = WebClient.create(vertx)
vertx.fileSystem().open("your-filename", new OpenOptions(), ar -> {
if (ar.succeeded()) {
def f = ar.result()
webClient.getAbs("http://127.0.0.1:8080/api/file")
.as(BodyCodec.pipe(f))
.send({
if (it.succeeded()) {
f.close()
webClient.close()
}
})
}
})
网友评论