-
大小: 173KB文件类型: .rar金币: 1下载: 0 次发布日期: 2021-06-10
- 语言: Java
- 标签:
资源简介
java+jquery文件上传,无刷新上传,显示上传进度(如5%),上传速度,已上传大小,剩于大小,已上传时间,统计上传所需时间,统计还剩多长时间
代码片段和文件信息
package servlet.upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class ProgressUploadServlet extends HttpServlet {
protected void doGet(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
resp.setHeader(“Cache-Control“ “no-store“);// 禁止浏览器缓存
resp.setHeader(“Pragrma“ “no-cache“);// 禁止浏览器缓存
resp.setDateHeader(“Expires“ 0);// 禁止浏览器缓存
UploadStatus status = (UploadStatus) req.getSession(true).getAttribute(
“uploadstatus“);
if (status == null) {
this.log(“没有上传文件!“);
return;
}
long startTime = status.getStartTime();
long currentTime = System.currentTimeMillis();
long time = (currentTime - startTime) / 1000 + 1; // 已传输的时间 单位:s
double velocity = ((double) status.getByteRead()) / (double) time;// 传输速度
// bytes/s
double totalTime = status.getContentLength() / velocity;// 估计时间
double timeLeft = totalTime - time;// 剩余时间
int percent = (int) (100 * (double) status.getByteRead() / (double) status
.getContentLength());// 已完成的百分比
double length = ((double) status.getByteRead() / 1024 / 1024);// 已完成数
// 单位:M
double totalLength = ((double) status.getContentLength()) / 1024 / 1024;// 总长度:
// 单位:M
// 格式:
// 百分比||已完成数(M)||文件总长度(M)||传输速率(K)||已用时间(S)||估计总时间(S)||估计剩余时间(S)||正在上传第几个文件
String value = percent + “||“ + length + “||“ + totalLength + “||“
+ velocity + “||“ + time + “||“ + totalTime + “||“ + timeLeft
+ “||“ + status.getItems();
resp.getWriter().print(value);// 输出给浏览器进度条
}
protected void doPost(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
UploadStatus status = new UploadStatus();// 上传状态
UploadListener listener = new UploadListener(status);// 监听器
req.getSession(true).setAttribute(“uploadstatus“ status);
ServletFileUpload upload = new ServletFileUpload(
new DiskFileItemFactory());// 解析
upload.setProgressListener(listener);// 设置上传的listener
try {
// upload.setSizeMax(10*1024*1024);//设置大小,超出则抛出FileUploadException
List itemList = upload.parseRequest(req);
for (Iterator it = itemList.iterator(); it.hasNext();) {
FileItem item = (FileItem) it.next();
if (!item.isFormField())// 如果是非表单数据
{
if(item.getName().length()<=0)return;//表示后面的文件域没有选中文件
this.log(“file size: “+((double)item.getSize()/1024/1024)+“M“);
this.log(“file type:“+item.
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 629 2011-12-10 14:48 upload\.settings\org.eclipse.jdt.core.prefs
文件 120 2011-12-10 14:48 upload\.settings\com.genuitec.jboss.ide.eclipse.xdoclet.run.prefs
文件 1040 2011-12-10 14:48 upload\.settings\com.genuitec.eclipse.j2eedt.core.prefs
文件 137 2011-12-10 14:48 upload\.settings\org.eclipse.ltk.core.refactoring.prefs
文件 799 2011-12-08 22:01 upload\src\servlet\upload\UploadStatus.java
文件 585 2011-12-08 22:09 upload\src\servlet\upload\UploadListener.java
文件 4228 2011-12-10 14:35 upload\src\servlet\upload\ProgressUploadServlet.java
文件 57779 2011-12-08 22:05 upload\WebRoot\WEB-INF\lib\commons-fileupload-1.2.1.jar
文件 109043 2011-12-08 22:05 upload\WebRoot\WEB-INF\lib\commons-io-1.4.jar
文件 1177 2011-12-10 14:43 upload\WebRoot\WEB-INF\classes\servlet\upload\UploadStatus.class
文件 774 2011-12-10 14:43 upload\WebRoot\WEB-INF\classes\servlet\upload\UploadListener.class
文件 4467 2011-12-10 14:43 upload\WebRoot\WEB-INF\classes\servlet\upload\ProgressUploadServlet.class
文件 707 2011-12-10 14:44 upload\WebRoot\WEB-INF\web.xm
文件 36 2011-12-04 10:39 upload\WebRoot\me
文件 2701 2011-12-10 13:47 upload\WebRoot\upload.jsp
文件 72174 2011-12-08 22:57 upload\WebRoot\js\jquery-1.4.2.min.js
文件 526 2011-12-08 22:05 upload\.classpath
文件 292 2011-12-10 14:48 upload\.myme
文件 1281 2011-12-04 10:39 upload\.project
文件 66 2011-12-10 14:48 upload\.myumldata
目录 0 2011-12-10 14:43 upload\WebRoot\WEB-INF\classes\servlet\upload
目录 0 2011-12-10 14:43 upload\WebRoot\WEB-INF\classes\servlet
目录 0 2011-12-08 22:00 upload\src\servlet\upload
目录 0 2011-12-04 10:39 upload\WebRoot\WEB-INF\lib
目录 0 2011-12-04 10:39 upload\WebRoot\WEB-INF\classes
目录 0 2011-12-04 10:41 upload\src\servlet
目录 0 2011-12-04 10:39 upload\WebRoot\WEB-INF
目录 0 2011-12-04 10:39 upload\WebRoot\me
目录 0 2011-12-08 22:57 upload\WebRoot\js
目录 0 2011-12-04 10:39 upload\.settings
............此处省略7个文件信息
评论
共有 条评论