【问题标题】:xmpp file upload using smack使用 smack 上传 xmpp 文件
【发布时间】:2018-11-01 02:15:14
【问题描述】:

我无法使用 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();
    }

【问题讨论】:

    标签: xmpp chat ejabberd smack


    【解决方案1】:

    你可以试试这个代码。

        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

    【讨论】:

      【解决方案2】:

      经过深入研究,我得到了解决方案。

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

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

      1. 您需要通过mtm或certificatepinning配置sslSocketFactory。
      OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder();
      SSLContext sslContext = JavaPinning.forPin(<PINNING_VALUE>);
      okHttpClientBuilder.writeTimeout(5, TimeUnit.MINUTES)
          .connectTimeout(5, TimeUnit.MINUTES)
          .readTimeout(5, TimeUnit.MINUTES);
      
      okHttpClientBuilder.sslSocketFactory(sslContext.getSocketFactory(), JavaPinning.trustManagerForPin(<PINNING_VALUE>));
      OkHttpClient client = okHttpClientBuilder.build();
      
      1. 启动okhttpclient并添加类似文件。
      Request request = new Request.Builder()
          .url(slot.getPutUrl())
          .put(RequestBody.create(MediaType.parse("application/octet-stream"), files))
              .build();
      
      1. 现在开始上传。
      client.newCall(request).enqueue(new Callback() {
      
          @Override
          public void onFailure(final Call call, final IOException e) {
              // Handle the error
              Log.i(log, "error " + e);
          }
      
          @Override
          public void onResponse(final Call call, final Response response) throws IOException {
              if (!response.isSuccessful()) {
                  // Handle the error
                  Log.i(log, "errored " + response);
              }
              Log.i(log, "success " + response);
              // Upload successful
          }
      });
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-12
        • 2011-06-27
        • 2015-01-20
        • 1970-01-01
        • 2018-02-25
        • 2023-04-07
        • 1970-01-01
        相关资源
        最近更新 更多