Hi Again @drd6y
I created a small driver program that you can use in order to see how File Transfer works with the Smack API.
In order for this program to work you gonna have to:
- create 2 account(name them however you want)
- change the host name and service names
- change the filename you want to transfer
SimpleSmackClient.java
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.filetransfer.FileTransferListener;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.FileTransferNegotiator;
import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
/**
*
-
@author ekakoll
*/
public class SimpleSmackClient {
public XMPPConnection connection;
private FileTransferManager manager;
public void login(String username, String password) throws XMPPException {
XMPPConnection.DEBUG_ENABLED = true;
ConnectionConfiguration config = new ConnectionConfiguration(“host”, 5222, “host”);
connection = new XMPPConnection(config);
connection.connect();
connection.login(username, password);
manager = new FileTransferManager(connection);
}
public void sendMessage(String message, String destination) throws XMPPException {
Chat chat = connection.getChatManager().createChat(destination,
new MessageListener() {
public void processMessage(Chat c, Message message) {
System.out.println(c.getParticipant() + " says: " + message.getBody());
}
});
chat.sendMessage(message);
}
public void logout() {
if (connection != null) {
connection.disconnect();
}
}
public void fileTransfer(String fileName, String destination) throws XMPPException {
// Create the file transfer manager
//FileTransferManager manager = new FileTransferManager(connection);
FileTransferNegotiator.setServiceEnabled(connection,true);
// Create the outgoing file transfer
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(destination);
// Send the file
transfer.sendFile(new File(fileName), “You won’t believe this!”);
try {
Thread.sleep(10000);
}
catch(Exception e){}
System.out.println("Status :: " + transfer.getStatus() + " Error :: " + transfer.getError() + " Exception :: " + transfer.getException());
System.out.println("Is it done? " + transfer.isDone());
}
public void fileReceiver(final boolean accept, final String fileName) {
// Create the file transfer manager
//final FileTransferManager manager = new FileTransferManager(connection);
// Create the listener
manager.addFileTransferListener(new FileTransferListener() {
public void fileTransferRequest(FileTransferRequest request) {
// broadcast something here. Wheather users want to accept file
// Check to see if the request should be accepted
if(accept) {
// Accept it
IncomingFileTransfer transfer = request.accept();
try {
transfer.recieveFile(new File(fileName));
System.out.println("File " + fileName + “Received Successfully”);
//InputStream input = transfer.recieveFile();
} catch (XMPPException ex) {
Logger.getLogger(SimpleSmackClient.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
// Reject it
request.reject();
}
}
});
}
}
User.java
/**
*
-
@author ekakoll
*/
public class User {
private String userName;
private String password;
protected static SimpleSmackClient ssc;
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
UserAkem.java:
package poc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.jivesoftware.smack.XMPPException;
/**
*
-
@author ekakoll
*/
public class UserAkem extends User {
public void init() throws Exception {
setUserName(“akem”);
setPassword(“akem”);
ssc = new SimpleSmackClient();
ssc.login(this.getUserName(), this.getPassword());
}
public void sendMessage(String message, String destination) throws XMPPException {
ssc.sendMessage(message, destination);
}
public void logout() {
ssc.logout();
}
public void fileTransfer(String fileName, String destination) throws XMPPException {
ssc.fileTransfer(fileName, destination);
}
public void fileReceiver(final boolean accept, final String fileName) {
ssc.fileReceiver(accept, fileName);
}
public static void main(String[] args) throws Exception {
UserAkem uak = new UserAkem();
uak.init();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String message = null;
while (!(message = br.readLine()).equalsIgnoreCase(“bye”)) {
ssc.sendMessage(message, “akolly@host”);
}
// Send file to user akolly
uak.fileTransfer(“D:\Documents and Settings\ekakoll\Desktop\Book Review.txt”,“akolly@host/Smack”);
//uak.logout();
}
}
UserAkolly.java
package poc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.jivesoftware.smack.XMPPException;
/**
*
-
@author ekakoll
*/
public class UserAkolly extends User {
public void init() throws Exception {
setUserName(“akolly”);
setPassword(“akolly”);
ssc = new SimpleSmackClient();
ssc.login(this.getUserName(), this.getPassword());
}
public void sendMessage(String message, String destination) throws XMPPException, XMPPException {
ssc.sendMessage(message, destination);
}
public void logout() {
ssc.logout();
}
public void fileTransfer(String fileName, String destination) throws XMPPException {
ssc.fileTransfer(fileName, destination);
}
public void fileReceiver(final boolean accept, final String fileName) {
ssc.fileReceiver(accept, fileName);
}
public static void main(String[] args) throws Exception {
UserAkolly uako = new UserAkolly();
uako.init();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String message = null;
while (!(message = br.readLine()).equalsIgnoreCase(“bye”)) {
ssc.sendMessage(message, “akem@host”);
// Receive new file. You can give it a new name
uako.fileReceiver(true, “D:\Documents and Settings\ekakoll\Desktop\Received Book Review.txt”);
}
//uako.logout();
}
}
N.B:
I hope this helps. If not let me know
Emmanuel