Java 类org.jivesoftware.smackx.filetransfer.FileTransfer 实例源码

项目:commune    文件:AbstractTransfer.java   
protected boolean checkTimeoutAndNotifyProgress(FileTransfer transfer) {

        long diff = transfer.getAmountWritten() - this.previousAmountWrittenValue;
        long currentTime = System.currentTimeMillis();
        long elapsed = currentTime - lastDataChangeTime;
        if ( diff > 0 ) {
            lastDataChangeTime = currentTime;
            this.previousAmountWrittenValue = transfer.getAmountWritten();
            // compute rate in kbytes per second
            if ( notifyProgress ) {
                double transferRate = diff / 1024D / (elapsed / 1000D);

                TransferProgress transferProgress = new TransferProgress(getHandle(), file.getName(), fileSize, 
                        transfer.getStatus(), transfer.getAmountWritten(), transfer.getProgress(), transferRate,
                        this.getClass().isAssignableFrom(OutgoingTransfer.class));

                Message message = 
                    createUpdateTransferProgressMessage(module.getContainerID(), listenerID, transferProgress);
                module.sendMessage(message);

            }
            return false;
        }
        return elapsed > inactivityTimeout;
    }
项目:yiim_v2    文件:FileUpload.java   
/**
     * 发送文件
     * 
     * @param connection
     * @param user
     * @param toUserName
     * @param file
     */
    public static void sendFile(final Context context,
            final Connection connection, final String toUser, final Uri uri,
            final String filePath, final MsgType msgType) {
        new Thread() {
            public void run() {
                XMPPConnection.DEBUG_ENABLED = true;
                // AccountManager accountManager;
                try {
                    // accountManager = connection.getAccountManager();
                    Presence pre = connection.getRoster().getPresence(toUser);
                    if (pre.getType() != Presence.Type.unavailable) {
                        if (connection.isConnected()) {
                            Log.d(TAG, "connection con");
                        }
                        // 创建文件传输管理器
//                      ServiceDiscoveryManager sdm = ServiceDiscoveryManager
//                              .getInstanceFor(connection);
//                      if (sdm == null)
//                          sdm = new ServiceDiscoveryManager(connection);

                        FileTransferManager manager = new FileTransferManager(
                                connection);
                        // 创建输出的文件传输
                        OutgoingFileTransfer transfer = manager
                                .createOutgoingFileTransfer(pre.getFrom());
                        // 发送文件
                        transfer.sendFile(new File(filePath),
                                msgType.toString());
                        while (!transfer.isDone()) {
                            if (transfer.getStatus() == FileTransfer.Status.in_progress) {
                                // 可以调用transfer.getProgress();获得传输的进度 
                                // Log.d(TAG,
                                // "send status:" + transfer.getStatus());
                                // Log.d(TAG,
                                // "send progress:"
                                // + transfer.getProgress());
                                if (mFileUploadListener != null) {
                                    mFileUploadListener.transProgress(context,
                                            uri, filePath,
                                            transfer.getProgress());
                                }
                            }
                        }
                        // YiLog.getInstance().i("send file error: %s",
                        // transfer.);
                        Log.d(TAG, "send status 1 " + transfer.getStatus());
                        if (transfer.isDone()) {
                            if (mFileUploadListener != null) {
                                mFileUploadListener.transDone(context, toUser,
                                        uri, msgType, filePath,
                                        transfer.getStatus());
                            }
                        }
                    }
                } catch (Exception e) {
                    Log.d(TAG, "send exception");
                    if (mFileUploadListener != null) {
                        mFileUploadListener.transDone(context, toUser, uri,
                                msgType, filePath, Status.error);
                    }
                }
            }
        }.start();
    }
项目:spark-svn-mirror    文件:SendFileTransfer.java   
private void updateBar(final OutgoingFileTransfer transfer, String nickname, String kBperSecond) {
    FileTransfer.Status status = transfer.getStatus();
    if (status == Status.negotiating_stream) {
        titleLabel.setText(Res.getString("message.negotiation.file.transfer", nickname));
    }
    else if (status == Status.error) {
        if (transfer.getException() != null) {
            Log.error("Error occured during file transfer.", transfer.getException());
        }
        progressBar.setVisible(false);
        progressLabel.setVisible(false);
        titleLabel.setText(Res.getString("message.unable.to.send.file", nickname));
        cancelButton.setVisible(false);
        retryButton.setVisible(true);
        showAlert(true);
    }
    else if (status == Status.in_progress) {
        titleLabel.setText(Res.getString("message.sending.file.to", nickname));
        showAlert(false);
        if (!progressBar.isVisible()) {
            progressBar.setVisible(true);
            progressLabel.setVisible(true);
        }

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    // 100 % = Filesize
                // x %   = Currentsize      
                    long p = (transfer.getBytesSent() * 100 / transfer.getFileSize() );
                    progressBar.setValue(Math.round(p));
                }
            });
        }
        catch (Exception e) {
            Log.error(e);
        }

        ByteFormat format = new ByteFormat();
        String bytesSent = format.format(transfer.getBytesSent());
        String est = TransferUtils.calculateEstimate(transfer.getBytesSent(), transfer.getFileSize(), _starttime, System.currentTimeMillis());

        progressLabel.setText(Res.getString("message.transfer.progressbar.text.sent", bytesSent, kBperSecond, est));
    }
    else if (status == Status.complete) {
        progressBar.setVisible(false);

        String fin = TransferUtils.convertSecondstoHHMMSS(Math.round(System.currentTimeMillis()-_starttime)/1000);
        progressLabel.setText(Res.getString("label.time", fin));
        titleLabel.setText(Res.getString("message.you.have.sent", nickname));
        cancelButton.setVisible(false);
        showAlert(true);
    }
    else if (status == Status.cancelled) {
        progressBar.setVisible(false);
        progressLabel.setVisible(false);
        titleLabel.setText(Res.getString("message.file.transfer.canceled"));
        cancelButton.setVisible(false);
        retryButton.setVisible(true);
        showAlert(true);
    }
    else if (status == Status.refused) {
        progressBar.setVisible(false);
        progressLabel.setVisible(false);
        titleLabel.setText(Res.getString("message.file.transfer.rejected", nickname));
        cancelButton.setVisible(false);
        retryButton.setVisible(true);
        showAlert(true);
    }

}