?? uploadlistener.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 + -