- send a file:
// Create the file transfer manager
FileTransferManager manager = new FileTransferManager(connection);
// Create the outgoing file transfer
FileTransferNegotiator.setServiceEnabled(connection, true);
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(jID);
// Send the file
transfer.sendFile(file, message);
while (!transfer.isDone()) {
EIPLogger.debug(transfer.getProgress() + " is done!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
EIPLogger.error("", e);
}
}
- receive the file:
// Create the file transfer manager
FileTransferNegotiator.setServiceEnabled(connection, true);
final FileTransferManager manager = new FileTransferManager(connection);
// Create the listener
manager.addFileTransferListener(new FileTransferListener() {
public void fileTransferRequest(FileTransferRequest request) {
// Accept it
try {
if(request.getFileName().endsWith(“bmp”))
request.reject();
IncomingFileTransfer transfer = request.accept();
System.out.println(“File received:”+request.getFileName());
transfer.recieveFile(new File(request.getFileName()));
while(!transfer.isDone())
{
System.out.println(“receiving the file…”);
if(transfer.getStatus().equals(Status.error)) {
System.out.println("ERROR!!! " + transfer.getError());
} else {
System.out.println(transfer.getStatus());
System.out.println(transfer.getProgress());
}
Thread.currentThread().sleep(1000);
}
System.out.println(“File Received”);
} catch (Exception e) {
e.printStackTrace();
}
}
});
- Output log:
receiving the file…
Initial
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
receiving the file…
Negotiating Stream
0.0
File Received
But the file i received is always 0kb, anyone who can explain what’s wrong with above codes?thanks a lot.