使用smack上传xmpp文件

时间:2018-05-22 10:33:04

标签: xmpp chat ejabberd smack

我无法使用smack客户端android在xmpp上传文件。 slot.puturl()返回" https://localhost:7443/httpfileupload/27c97df7-dbbf-47ff-b19a-3ac624e51cf0/1.jpg"

 HttpFileUploadManager manager = HttpFileUploadManager.getInstanceFor(mConnection);
    try {
        Slot slot = manager.requestSlot(path, 10000);

        uploadFileToSlot(new File(path), slot);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
        e.printStackTrace();
    } catch (SmackException e) {
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码。

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);

}

参考File Transfer not working smack 4.1 android

答案 1 :(得分:0)

经过非常深入的研究,我找到了解决方案。

该HttpFileUploadManager仅用于从服务器请求插槽。

使用httpclient或okhttpclient获得插槽请求url上传文件后。 对于okhttpclient:

  1. 您需要通过mtm或certificatepinning配置sslSocketFactory。
ResultSet
  1. 启动okhttpclient并添加类似文件。
ResultSet
  1. 现在开始上传。
ResultSet

希望它对您有帮助。