AnsweredAssumed Answered

file transfer question

Question asked by yab on Apr 17, 2017
Latest reply on Apr 18, 2017 by yab

I use openfire+smack,

When I using smack to transfer file. I got a error in file sender. And in this time, I find the sender will lost connection from openfire console.

 

 

 

 

the receiver:

 

final Thread thread = new Thread() {

  @Override

  public void run() {

  if (!file.exists()) {

  Log.i("have no file", file.getPath());

  File dir = new File(BaseApplication.FILE_PATH

  + File.separator + userimei

  + File.separator);

  if (!dir.exists()) {

  dir.mkdirs();

  }

  try {

  file.createNewFile();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  try {

  transfer.recieveFile(file);

  } catch (SmackException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  double percents = 0.0;

  double pr=0;

  while (!transfer.getStatus().equals(Status.complete)) {

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

  Log.e("error",

  "ERROR!!! " + transfer.getError());

  transfer.cancel();

  try {

  transfer.recieveFile(file);

  } catch (SmackException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  } else {

  if(pr!=0&&pr==transfer.getProgress())

  {

  transfer.cancel();

  break;

  }

  pr=transfer.getProgress();

  Log.i("pr", "" + pr);

  if (transfer.getStatus().equals(

  Status.in_progress)) {

  percents = ((int) (transfer.getProgress() * 10000)) / 100.0;

  pr=transfer.getProgress();

  Log.i("Transfer status is: ",

  "" + transfer.getStatus());

  Log.i(request.getFileName(), "" + percents);

  // fs.currentSize = currentLength;

  fs.percent = (int) percents;

  }

  try {

  Thread.sleep(1000);

  } catch (InterruptedException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  }

  if (transfer.isDone()) {

  Log.i("info", "Done+status:" + transfer.getStatus());

  Log.i("info",

  "Done+process:" + transfer.getProgress());

  }

  }

  };

 

 

the sender:

 

try {

  outgoingFileTransfer.sendFile(insfile, fileid);

  Log.i("fileid", fileid);

  while (!outgoingFileTransfer.isDone()) {

  if (outgoingFileTransfer.getStatus().equals(

  FileTransfer.Status.error)) {

  Log.e(tag, "send failed");

  } else {

  Log.i(tag, "status:" + outgoingFileTransfer.getStatus()

  + "|progress:" + outgoingFileTransfer.getProgress());

  }

  Thread.sleep(1000);

  }

  } catch (SmackException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  Log.i(tag, "文件发送失败");

  } catch (InterruptedException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

Outcomes