File Transfer not working smack 4.1 android

前端 未结 2 747
梦如初夏
梦如初夏 2020-12-11 13:57

Currently I am playing around with the new Smack 4.1 that offers full support for android. Sending and receiving messages is no problem, works fine. But now, I get stuck on

2条回答
  •  轮回少年
    2020-12-11 14:50

    I got it after very research.

    FileTransferManager ftm1 = FileTransferManager.getInstanceFor(connection);
        FileTransferManager ftm2 = FileTransferManager.getInstanceFor(connection2);
    
        ftm2.addFileTransferListener(new FileTransferListener() {
            @Override
            public void fileTransferRequest(FileTransferRequest request) {
                IncomingFileTransfer ift = request.accept();
                try {
                    InputStream is = ift.recieveFile();
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    int nRead;
                    byte[] buf = new byte[1024];
                    while ((nRead = is.read(buf,  0, buf.length)) != -1) {
                        os.write(buf, 0, nRead);
                    }
                    os.flush();
                    dataReceived = os.toByteArray();
                } catch (SmackException | IOException | XMPPErrorException e) {
                    e.printStackTrace();
                }
                if (Arrays.equals(dataToSend, dataReceived)) {
                    System.out.println("Received data matches send data. \\o/");
                } else {
                    System.err.println("Recieved data DOES NOT match send data. :(");
                }
            }
        });
    
        OutgoingFileTransfer oft = ftm1.createOutgoingFileTransfer(XmppStringUtils.completeJidFrom(USER, SERV, "resourse"));
        oft.sendStream(new ByteArrayInputStream(dataToSend), "hello.txt", dataToSend.length, "A greeting");
        outerloop: while (!oft.isDone()) {
            switch (oft.getStatus()) {
            case error:
                System.out.println("Filetransfer error: " + oft.getError());
                break outerloop;
            default:
                System.out.println("Filetransfer status: " + oft.getStatus() + ". Progress: " + oft.getProgress());
                break;
            }
            Thread.sleep(1000);
        }
    
        connection.disconnect();
        connection2.disconnect();
        Thread.sleep(1000);
    }
    

    the one connection is sending file and another connection is receiving this is working code.

提交回复
热议问题