uploadlistener.java

来自「JSP版本的多文件上传。可同时选择多个文件进行上传。」· Java 代码 · 共 70 行

JAVA
70
字号
package com.blue.util;

import org.apache.commons.fileupload.ProgressListener;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.ScriptProxy;

import com.blue.beans.ProgressInfo;

public class UploadListener implements ProgressListener {

	private static final long UPDATE_THRESHOLD = 50 * 1024L; // 

	private static WebContext wctx = null;

	private ScriptProxy sp = null;

	private long megaBytes = -1;

	private ProgressInfo pi = null;

	public UploadListener() {
		wctx = WebContextFactory.get();
	}

	public UploadListener(ProgressInfo pi) {
		this.pi = pi;
	}

	public void update(long pBytesRead, long pContentLength, int pItems) {

		try {
			// Make the upload more slowly, so we can see the progress bar for small file.
			// In Prod env. it should be removed.
			Thread.sleep(10);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		if(pBytesRead + 2000 > pContentLength){
			pi.setBytesRead(pContentLength);
			sp.addFunctionCall("updateProgress", pi);
			return;
		}
		long mBytes = pBytesRead / UPDATE_THRESHOLD;
		if (megaBytes == mBytes && megaBytes > 0) {
			return;
		}
		
		megaBytes = mBytes;
		pi.setBytesRead(pBytesRead);
		pi.setFileIndex(pItems);
		pi.setTotalSize(pContentLength);

		// WebContext wctx = WebContextFactory.get();
		// org.directwebremoting.proxy.dwr.Util utilThis = new
		// org.directwebremoting.proxy.dwr.Util(wctx.getScriptSession());
		// utilThis.setValue("text", String.valueOf(pBytesRead));
		if(sp == null){
			sp = new ScriptProxy(wctx.getScriptSessionsByPage(wctx.getCurrentPage()));
		}
		sp.addFunctionCall("updateProgress", pi);

	}

	public void go() {
		System.out.println("---------go!");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?