-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Server.java
43 lines (38 loc) · 1.16 KB
/
Server.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package io.vertx.example.core.http.upload;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Launcher;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.AsyncFile;
import io.vertx.core.file.OpenOptions;
import io.vertx.core.streams.Pipe;
import java.util.UUID;
/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class Server extends AbstractVerticle {
public static void main(String[] args) {
Launcher.executeCommand("run", Server.class.getName());
}
@Override
public void start() throws Exception {
vertx.createHttpServer().requestHandler(req -> {
Pipe<Buffer> pipe = req
.pipe()
.endOnComplete(true);
String filename = UUID.randomUUID() + ".uploaded";
vertx.fileSystem()
.open(filename, new OpenOptions())
.transform(ar -> {
if (ar.succeeded()) {
return pipe.to(ar.result()).onComplete(ar2 -> {
System.out.println("Uploaded to " + filename);
});
} else {
return req.response()
.setStatusCode(500)
.end("Could not upload file");
}
});
}).listen(8080);
}
}