powered by Jive Software

Problem about file transfer

Hi everyone,

I have some questions about file transfer on openfire. I was following steps on offical documents.

http://www.igniterealtime.org/builds/smack/docs/latest/documentation/extensions/ index.html

Here is my code:

  1. send file
public void onClick(View v){
                              FileTransferManager manager=new FileTransferManager(ConstantUtils.conn2);
                              OutgoingFileTransfer transfer=manager.createOutgoingFileTransfer(ConstantUtils.chatname+"@srong/Smack");
                              File file=new File("/mnt/sdcard/test1.jpg");
                              System.out.println("begin to send file");
                              try {                                         transfer.sendFile(file,"hahaha");
                              } catch (XMPPException e) {
                                        // TODO Auto-generated catch block                                         e.printStackTrace();
                              }
                              System.out.println("done(send file)");
                              while(!transfer.isDone()){
                                        if(transfer.getStatus().equals(Status.error)){
                                                  System.out.println("ERROR!!!"+transfer.getError());
                                        }else if(transfer.getStatus().equals(Status.cancelled)||transfer.getStatus().equals(Status.refused)){
                                                  System.out.println("Canceled!!!"+transfer.getError());
                                        }
                                        try{
                                                  Thread.sleep(1000L);
                                        }catch(InterruptedException e){
                                                  e.printStackTrace();
                                        }
                              }
                              if(transfer.getStatus().equals(Status.refused)||transfer.getStatus().equals(Status.error)||transfer.getStatus().equals(Status.cancelled)){
                                        System.out.println("refused cancelled error "+transfer.getError());
                              }else{
                                        System.out.println("Success");
                              }
                    }

2)receive file

FileTransferManager manager=new FileTransferManager(ConstantUtils.conn2);
                              manager.addFileTransferListener(new FileTransferListener(){
                                        public void fileTransferRequest(final FileTransferRequest request){
                                                  new Thread(){
                                                            public void run(){
                                                                      IncomingFileTransfer transfer=request.accept();
                                                                      File mf=Environment.getExternalStorageDirectory();
                                                                      File file=new File(mf.getAbsoluteFile()+"/DCIM/Camera/"+transfer.getFileName());
                                                                      try{
                                                                                transfer.recieveFile(file);
                                                                                while(!transfer.isDone()){
                                                                                          try{
                                                                                                    Thread.sleep(1000L);
                                                                                          }catch(Exception e){
                                                                                                    e.printStackTrace();
                                                                                          }
                                                                                          if(transfer.getStatus().equals(Status.error)){
                                                                                                    System.out.println("error"+transfer.getError());
                                                                                          }
                                                                                          if(transfer.getException()!=null){
                                                                                                    transfer.getException().printStackTrace();
                                                                                          }
                                                                                }
                                                                      }catch(Exception e){
                                                                                System.out.println(e.getMessage());
                                                                      }
                                                            };
                                                  }.start();
                                        }
                              });

While eclipse send:

refused cancelled error null

It stopped when I send a file at

if(transfer.getStatus().equals(Status.refused)||transfer.getStatus().equals(Status.error)||transfer.getStatus().equals(Status.cancelled)){
                                        System.out.println("refused cancelled error "+transfer.getError());
                              }else{
                                        System.out.println("Success");
                              }

But the error is null. I don’t know how to slove it.

Thank you all.

Strong Su

You can Try this :::

For Receive file ::

FileTransferManager file = new FileTransferManager(connection);

file.addFileTransferListener(new FileTransferListener() {

@Override

public void fileTransferRequest(

final FileTransferRequest request) {

// TODO Auto-generated method stub

Thread thread = new Thread(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

// TODO Auto-generated method stub

Log.e(“Recieve File”,

“new file transfere request new file transfere request new file transfere request”);

Log.e(“file request”,

“from” + request.getRequestor());

IncomingFileTransfer transfer = request

.accept();

Log.e(“Recieve File alert dialog”, “accepted”);

try {

File root = android.os.Environment

.getExternalStorageDirectory();

File dir = new File(root.getAbsolutePath()

  • “/download”);

dir.mkdirs();

transfer.recieveFile(new File("/sdcard/"

  • request.getFileName()));

while (!transfer.isDone()

|| (transfer.getProgress() < 1)) {

Thread.sleep(1000);

Log.e(“Recieve File alert dialog”,

"still receiving : "

  • (transfer

.getProgress())

  • " status "

  • transfer.getStatus());

if (transfer.getStatus().equals(

Status.error)) {

// Log.i(“Error file”,

// transfer.getError().getMessage());

Log.e(“Recieve File alert dialog”,

"cancelling still receiving : "

  • (transfer

.getProgress())

  • " status "

  • transfer

.getStatus());

transfer.cancel();

break;

}

}

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

thread.start();

}

});

2) For Send File

----------------------------------------

public static void setFilename(String filename, final String filepath) {

@SuppressWarnings(“deprecation”)

String FileName = URLDecoder.decode(filename);

demo.globr.Utils.Log.print("Filename :: " + FileName);

String FilePath = filepath.substring(7, filepath.length());

demo.globr.Utils.Log.print("filepath :: " + FilePath);

Thread t = new Thread(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

ProviderManager.getInstance().addIQProvider(“query”,

                    "[http://jabber.org/protocol/bytestreams](http://jabber.org/protocol/bytestreams)",

new BytestreamsProvider());

ProviderManager.getInstance().addIQProvider(“query”,

                    "[http://jabber.org/protocol/disco#items](http://jabber.org/protocol/disco#items)",

new DiscoverItemsProvider());

ProviderManager.getInstance().addIQProvider(“query”,

                    "[http://jabber.org/protocol/disco#info](http://jabber.org/protocol/disco#info)",

new DiscoverInfoProvider());

XMPPConnection connection = XMPPLogic.getInstance()

.getConnection();

if (connection != null && connection.isConnected()) {

FileTransferManager manager = new FileTransferManager(

connection);

OutgoingFileTransfer transfer = manager

.createOutgoingFileTransfer(receiverId + “/Smack”);

File file = new File(filepath);

try {

transfer.sendFile(file, “test_file”);

} catch (XMPPException e) {

e.printStackTrace();

}

while (!transfer.isDone()) {

if (transfer.getStatus().equals(Status.error)) {

Log.e("ERROR!!! ", transfer.getError().toString());

} else if (transfer.getStatus()

.equals(Status.cancelled)

|| transfer.getStatus().equals(Status.refused)) {

Log.e("Cancelled!!! ", transfer.getError()

.toString());

}

try {

Thread.sleep(1000L);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

if (transfer.getStatus().equals(Status.refused)

|| transfer.getStatus().equals(Status.error)

|| transfer.getStatus().equals(Status.cancelled)) {

} else {

}

}

}

});

t.start();

}

Thanks for your reply, rajesh

And I have a question about your code. What’s the defination of XMPPLogic?

By comparing the codes, I found some difference. The “connection” is XMPPConnection in your case. While it’s org.jivesoftware.smack.Connection in mine.

Thank you,

Strong Su