如何做上传进度?
你用的是什么语言开发的?如果是java的话,可以继承一个inputstream在里面实现一些和进度相关的处理,然后将该inputstream传给putObject就可以实现了,下面是简单的代码示例:
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'; } }}
赞0
踩0