1

如何在 Android 中使用 Smack 和 Openserver 接收传输的文件?以下是我用来接收文件的代码。但是出现错误并且无法记录任何文件。

ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
        if(sdm == null)
        {
            sdm = new ServiceDiscoveryManager(connection);
            sdm.addFeature("http://jabber.org/protocol/disco#info");
            sdm.addFeature("http://jabber.org/protocol/disco#item");
            sdm.addFeature("jabber:iq:privacy");
            XMPPConnection.DEBUG_ENABLED = true;
       }

        FileTransferManager manager=new FileTransferManager(connection);
        FileTransferNegotiator.setServiceEnabled(connection, true);
        manager.addFileTransferListener(new FileTransferListener() 
        {    
          @Override
          public void fileTransferRequest(final FileTransferRequest request) 
          {
             Log.i("Recieve File","new file transfere request  new file transfere request   new file transfere request");
             Log.i("file request","from" + request.getRequestor());
            // TODO Auto-generated method stub
            new Thread()
            {
                   @Override
                   public void run() 
                   {
                     IncomingFileTransfer transfer = request.accept();
                     File mf = Environment.getExternalStorageDirectory();
                     File file = new File(mf.getAbsoluteFile()+"/DCIM/Camera/" + transfer.getFileName());
                     try
                     {
                         System.out.println("<====== Receives ======>");
                         transfer.recieveFile(file);
                         while (!transfer.isDone()|| (transfer.getProgress() < 1)) {
                             Thread.sleep(1000);
                             Log.i("Recieve File alert dialog","still receiving : "+ (transfer.getProgress()) + " status " + transfer.getStatus());
                             if (transfer.getStatus().equals("error")) {
                                  Log.i("Recieve File alert dialog", "cancelling still receiving : "+ (transfer.getProgress())+ " status "+ transfer.getStatus());
                                  transfer.cancel();
                                  break;
                             }
                          }
                    }catch (Exception e){
                           Log.e("", e.getMessage());
                    }          
                  }
            }.start();
          }
        });

发送文件时出现以下错误。

02:48:58 PM RCV  (1079277904): <iq type="error" id="6gQg0-6" to="admin-pc/cc4dd310" from="2222222222@admin-pc/Smack"><si xmlns="http://jabber.org/protocol/si" id="jsi_3030055238839806007" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="sdcard"><desc>test</desc></file><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="form"><field var="stream-method" type="list-single"><option><value>http://jabber.org/protocol/bytestreams</value></option><option><value>http://jabber.org/protocol/ibb</value></option></field></x></feature></si><error code="405" type="cancel"><not-allowed xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>
4

1 回答 1

0

First off, you need to use version 3.1 or 3.3 as there is a known issue with 3.2. then check out this article or this one for guidance on coding the said transfer. Hope this helps.

于 2013-05-11T08:29:06.473 回答