How to transfer File using vertx

626 views
Skip to first unread message

뒷태지존오명운

unread,
Jun 27, 2013, 5:01:05 AM6/27/13
to ve...@googlegroups.com
I'm testing transferring a file using vertx via NetSocket, not via HTTP like sendfile example.

I created 2 verticles, which are Requestor/Responsor.

When I tried to transfer a file, the file is not downloaded correctly, 
which means that AsyncResult.succeeded() returns true but the length of the downloaded file is not equal to the original file.

I guess there might be some special methods to handle the stream or buffer, but it's hard to find in javadoc.

Please let me know how to do it correctly.

Below are source files I made.

-------------------------------------------------------------
FileResponsor.java

public class FileResponsor extends Verticle {

@Override
public void start() throws Exception {

Vertx vertx = Vertx.newVertx();
System.out.println("FileResponsor vertx created : " + vertx);
Handler<NetSocket> nSocketHandler = new Handler<NetSocket>() {
public void handle(final NetSocket nSocket) {
nSocket.sendFile("D:\\HearBeatServerMain.java");
}
};
vertx.createNetServer().connectHandler(nSocketHandler).listen(17002);
}

}

-------------------------------------------------------------
FileRequestor.java

public class FileRequestor extends Verticle {
private String urlServer1 = "127.0.0.1";

@Override
public void start() throws Exception {

Vertx vertx = Vertx.newVertx();
System.out.println("FileRequestor vertx created : " + vertx);
final FileSystem fileSystem = vertx.fileSystem();
Handler<NetSocket> netSocketHandler = new Handler<NetSocket>() {
public void handle(NetSocket nSocket) {
nSocket.dataHandler(new Handler<Buffer>() {
public void handle(Buffer buffer) {
fileSystem.writeFile("D:\\downloadedfile", buffer, new AsyncResultHandler<Void>() {

public void handle(AsyncResult asyncResult) {
if ( asyncResult.succeeded() ) {
System.out.println("Download Succeeded");
} else if ( asyncResult.failed() ) {
System.out.println("Download Failed");
}
}
});
}
});
}
};
vertx.createNetClient().connect(17002, urlServer1, netSocketHandler);
}

}

Tim Fox

unread,
Jun 27, 2013, 5:06:06 AM6/27/13
to ve...@googlegroups.com
Please use gist.github.com for code snippets.

Also pls take a look at the upload example in the 1.3.1 distro /
vertx-examples repo

뒷태지존오명운

unread,
Jun 30, 2013, 9:10:50 PM6/30/13
to ve...@googlegroups.com
Thank you .
I didn't know about gist, I will use it later on.

2013년 6월 27일 목요일 오후 6시 6분 6초 UTC+9, Tim Fox 님의 말:
Reply all
Reply to author
Forward
0 new messages