⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadlistener.java

📁 简单实用的ajax实例,包含动态数据加载、电子商务应用、自动及定时业务等小型实例
💻 JAVA
字号:
package ajax.upload;

import javax.servlet.http.HttpServletRequest;

public class UploadListener implements OutputStreamListener {
	private HttpServletRequest request;

	private long delay = 0;// 延迟时间,用于Debug(毫秒)

	private int totalSize = 0;// 上传文件尺寸

	private int totalBytesRead = 0;// 已读取字节数

	// 构造方法,同时设置了请求和延迟时间
	public UploadListener(HttpServletRequest request, long debugDelay) {
		this.request = request;
		this.delay = debugDelay;
		totalSize = request.getContentLength();
	}

	// 上传开始
	public void start() {
		updateUploadInfo("start");
	}

	// 读取字节
	public void bytesRead(int bytesRead) {
		totalBytesRead = totalBytesRead + bytesRead;
		updateUploadInfo("progress");

		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	// 上传错误
	public void error(String message) {
		updateUploadInfo("error");
	}

	// 上传结束
	public void done() {
		updateUploadInfo("done");
	}

	// 更新上传信息
	private void updateUploadInfo(String status) {
		request.getSession().setAttribute("uploadInfo",
				new UploadInfo(totalSize, totalBytesRead, status));
	}

}

⌨️ 快捷键说明

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