由于经常用户上传的时候处于提示:上传中,问加你稍大会员就以为卡住了,请问如何做一个上传进度来显示状态呢?求解!在线等。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
public class ProgressReportingInputStream extends FilterInputStream {
    private static final int NOTIFICATION_THRESHOLD = 8 * Constants.KB;
    private final ProgressListener listener;
    
    private int unnotifiedByteCount;
    public ProgressReportingInputStream(final InputStream in, final ProgressListener listener) {
        super(in);
        this.listener = listener;
    }
    @Override
    public int read() throws IOException {
        int data = super.read();
        if (data != -1) notify(1);
        return data;
    }
    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int bytesRead = super.read(b, off, len);
        if (bytesRead != -1) notify(bytesRead);
        return bytesRead;
    }
    @Override
    public void close() throws IOException {
        if (unnotifiedByteCount > 0) {
            listener.progressChanged(new ProgressEvent(unnotifiedByteCount));
            unnotifiedByteCount = 0;
        }
        super.close();
    }
    
    private void notify(int bytesRead) {
        unnotifiedByteCount  = bytesRead;
        if (unnotifiedByteCount >= NOTIFICATION_THRESHOLD) {
            listener.progressChanged(new ProgressEvent(unnotifiedByteCount));
            unnotifiedByteCount = 0;
        }
    }
    
    
}
public interface ProgressListener {
    public void progressChanged(ProgressEvent progressEvent);
}
public class ProgressEvent {
    public static final int DEFAULT_EVENT_CODE = 0;
    public static final int STARTED_EVENT_CODE   = 1;
    public static final int COMPLETED_EVENT_CODE = 2;
    public static final int FAILED_EVENT_CODE    = 4;
    public static final int CANCELED_EVENT_CODE  = 8;
    private int bytesTransfered;
    
    private int eventCode;
    
    public ProgressEvent(int bytesTransfered) {
        this.bytesTransfered = bytesTransfered;
    }
    public void setBytesTransfered(int bytesTransfered) {
        this.bytesTransfered = bytesTransfered;
    }
    public int getBytesTransfered() {
        return bytesTransfered;
    }
    public int getEventCode() {
        return eventCode;
    }
    public void setEventCode(int eventType) {
        this.eventCode = eventType;
    }
}
public class ProgressListenerImp implements ProgressListener {
    private final int SPEED_UPADATE_INTERVAL = 300;
    private IconTableModel iconTableModel;
    private String taskName;
    private long totalSize = 0;
    private long curSize = 0;
    private long newUploadSize = 0;
    private long lastTime = 0;
    private String taskType;
    private boolean isFailed = false;
    @Override
    public void progressChanged(ProgressEvent progressEvent) {
        switch (progressEvent.getEventCode()) {
        case ProgressEvent.DEFAULT_EVENT_CODE:
            int ratio = -1,
            speed = -1;
            long curTime = new Date().getTime();
            curSize  = progressEvent.getBytesTransfered();
            newUploadSize  = progressEvent.getBytesTransfered();
            if (totalSize > 0) {
                ratio = (int) (curSize * 100 / totalSize);
            }
            if (curTime - lastTime > SPEED_UPADATE_INTERVAL) {
                speed = (int) (newUploadSize * 1000 / (curTime - lastTime));
                lastTime = curTime;
                newUploadSize = 0;
            }
            if (!isFailed) {
                iconTableModel.setValueAt(taskName, ratio, taskType, speed);
            }
            break;
        case ProgressEvent.STARTED_EVENT_CODE:
            iconTableModel.setValueAt(taskName, 0, taskType, 0);
            lastTime = new Date().getTime();
            break;
        case ProgressEvent.COMPLETED_EVENT_CODE:
            iconTableModel.setValueAt(taskName, 100, "Completed", 0);
            iconTableModel.removeTask(taskName);
            break;
        case ProgressEvent.CANCELED_EVENT_CODE:
            iconTableModel.setValueAt(taskName, -1, "Canceled", 0);
            break;
        case ProgressEvent.FAILED_EVENT_CODE:
            isFailed = true;
            iconTableModel.setValueAt(taskName, -1, "Failed", 0);
            break;
        }
    }
    public ProgressListenerImp(ResourceManager resourceManager,
            IconTableModel iconTableModel, String taskName, long totalSize,
            int taskType) {
        super();
        this.iconTableModel = iconTableModel;
        this.taskName = taskName;
        this.totalSize = totalSize;
        if (taskType == Utils.CREATE_FOLDER
                || taskType == Utils.UPLOAD_FILE) {
            this.taskType = "Uploading";
        } else {
            this.taskType = "Downloading";
        }
    }
}