/**
* 发送文件
* @param con 用户连接对象
* @param fullJID 用户jid 形式如:demo@serviceName/Spark 2.6.3
* @param filePath 文件路径
*/
public boolean sendFiles(XMPPConnection con,String fullJID,String filePath){
File files=new File(filePath);
FileTransferManager fileManager=new FileTransferManager(con);
OutgoingFileTransfer sendfile=fileManager.createOutgoingFileTransfer(fullJID);
try {
sendfile.sendFile(files, "Sending file");
return true;
} catch (XMPPException e) {
e.printStackTrace();
return false;
}
}
/**
* 接收文件
* @param con 用户连接对象
* @param filePath 文件路径
*/
public void receiveFiles(XMPPConnection con,final String filePath){
FileTransferManager fileManager=new FileTransferManager(con);
fileManager.addFileTransferListener(new FileTransferListener() {
@Override
public void fileTransferRequest(FileTransferRequest prequest) {
//System.out.println("The file received from: " + prequest.getRequestor());
System.out.println("filePath:"+filePath+"||FileName:"+prequest.getFileName());
file = new File(filePath +"\\" +prequest.getFileName());
request = prequest;
IncomingFileTransfer infiletransfer =request.accept();
try {
infiletransfer.recieveFile(file);
System.out.println("接收成功!");
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
}