博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件上传---动作条
阅读量:7014 次
发布时间:2019-06-28

本文共 11925 字,大约阅读时间需要 39 分钟。

hot3.png

  利用Apache commons fileupload上传文件,直接显示其完成的进度条。----示例代码源自《JAVA WEB王者归来》

  1 首先要显示动作条要利用Ajax的异步请求,使得在没有完成时,不会刷新本页,而是局部的刷新。如果没有指定form的定向页面,默认是刷新本页,正常我们提交一个form刷新本页,在没有完成请求前是显示空白的网页,这里我们指定他刷新一个不显示的区域,就要用到form的属性target。

    

 

  2 我们创建一个status的bean,用来保存一些状态信息,比如名字,读取的长度,读取的字节大小,时间等等。

public class UploadStatus {    private long bytesRead;    private long contentLength;    private int items;    private long startTime = System.currentTimeMillis();    public long getBytesRead() {        return bytesRead;    }    public void setBytesRead(long bytesRead) {        this.bytesRead = bytesRead;    }    public long getContentLength() {        return contentLength;    }    public void setContentLength(long contentLength) {        this.contentLength = contentLength;    }    public int getItems() {        return items;    }    public void setItems(int items) {        this.items = items;    }    public long getStartTime() {        return startTime;    }    public void setStartTime(long startTime) {        this.startTime = startTime;    }}

 

  3 创建一个监听器实时的获取状态信息

public class UploadListener implements ProgressListener {    private UploadStatus status;    public UploadListener(UploadStatus status){        this.status = status;    }    public void update(long bytesRead,long contentLength,int items){        status.setBytesRead(bytesRead);        status.setContentLength(contentLength);        status.setItems(items);    }}

 

  4 form表单用pos方法提交后,在doPost里面获取状态信息,保存到session中。在本页面,在自动用get方法读取session中的信息,返回给网页,网页动态的进行数据的显示。

  doPost

UploadStatus status = new UploadStatus();        UploadListener listener = new UploadListener(status);        request.getSession(true).setAttribute("uploadStatus",status);                ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());                upload.setProgressListener(listener);

  doGet

long startTime = status.getStartTime();        long currentTime = System.currentTimeMillis();        long time = (currentTime - startTime)/1000 + 1;                double velocity = ((double)status.getBytesRead())/(double)time;                double totalTime = status.getContentLength()/velocity;                double timeLeft = totalTime - time;                int percent = (int)(100*(double)status.getBytesRead()/(double)status.getContentLength());                double length = ((double)status.getBytesRead())/1024/1024;                double totalLength = ((double)status.getContentLength())/1024/1024;                String value = percent +"||" + length + "||" +totalLength+"||"+                        velocity+"||"+time+"||"+totalTime+"||"+timeLeft+"||"+status.getItems();                response.getWriter().println(value);

js

全部代码:

progressFileupload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'progressFileupload.jsp' starting page        
View Code

UploadStatus.java

package com.test.temp;public class UploadStatus {    private long bytesRead;    private long contentLength;    private int items;    private long startTime = System.currentTimeMillis();    public long getBytesRead() {        return bytesRead;    }    public void setBytesRead(long bytesRead) {        this.bytesRead = bytesRead;    }    public long getContentLength() {        return contentLength;    }    public void setContentLength(long contentLength) {        this.contentLength = contentLength;    }    public int getItems() {        return items;    }    public void setItems(int items) {        this.items = items;    }    public long getStartTime() {        return startTime;    }    public void setStartTime(long startTime) {        this.startTime = startTime;    }}
View Code

UploadListener.java

package com.test.temp;import org.apache.commons.fileupload.ProgressListener;public class UploadListener implements ProgressListener {    private UploadStatus status;    public UploadListener(UploadStatus status){        this.status = status;    }    public void update(long bytesRead,long contentLength,int items){        status.setBytesRead(bytesRead);        status.setContentLength(contentLength);        status.setItems(items);    }}
View Code

PorgressUploadServlet.java

package com.test.hello;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;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;import com.test.temp.UploadListener;import com.test.temp.UploadStatus;public class ProgressUploadServlet extends HttpServlet {    /**     * Constructor of the object.     */    public ProgressUploadServlet() {        super();    }    /**     * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control","no-store"); response.setHeader("Pragrma","no-cache"); response.setDateHeader("Expires",0); UploadStatus status = (UploadStatus) request.getSession(true).getAttribute("uploadStatus"); if(status == null){ response.getWriter().println("没有上传信息"); return; } long startTime = status.getStartTime(); long currentTime = System.currentTimeMillis(); long time = (currentTime - startTime)/1000 + 1; double velocity = ((double)status.getBytesRead())/(double)time; double totalTime = status.getContentLength()/velocity; double timeLeft = totalTime - time; int percent = (int)(100*(double)status.getBytesRead()/(double)status.getContentLength()); double length = ((double)status.getBytesRead())/1024/1024; double totalLength = ((double)status.getContentLength())/1024/1024; String value = percent +"||" + length + "||" +totalLength+"||"+ velocity+"||"+time+"||"+totalTime+"||"+timeLeft+"||"+status.getItems(); response.getWriter().println(value); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UploadStatus status = new UploadStatus(); UploadListener listener = new UploadListener(status); request.getSession(true).setAttribute("uploadStatus",status); ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); upload.setProgressListener(listener); try{ List itemList = upload.parseRequest(request); for(Iterator it = itemList.iterator();it.hasNext();){ FileItem item = (FileItem)it.next(); if(item.isFormField()){ System.out.println("FormField:"+item.getFieldName()+"="+item.getString()); }else{ System.out.println("File:"+item.getName()); String fileName = item.getName().replace("/","\\"); fileName = fileName.substring(fileName.lastIndexOf("\\")); File saved = new File("D:\\upload_temp",fileName); saved.getParentFile().mkdirs(); InputStream ins = item.getInputStream(); OutputStream ous = new FileOutputStream(saved); byte[] tmp = new byte[1024]; int len = -1; while((len = ins.read(tmp)) != -1){ ous.write(tmp,0,len); } ous.close(); ins.close(); response.getWriter().println("已保存文件:"+saved); } } }catch(Exception e){ response.getWriter().println("上传发生错误:"+e.getMessage()); } // response.setContentType("text/html");// PrintWriter out = response.getWriter();// out// .println("");// out.println("");// out.println(" A Servlet");// out.println(" ");// out.print(" This is ");// out.print(this.getClass());// out.println(", using the POST method");// out.println(" ");// out.println("");// out.flush();// out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }}
View Code

web.xml

This is the description of my J2EE component
This is the display name of my J2EE component
PostServlet
com.test.hello.PostServlet
This is the description of my J2EE component
This is the display name of my J2EE component
test
com.test.hello.test
This is the description of my J2EE component
This is the display name of my J2EE component
UploadServlet
com.test.hello.UploadServlet
This is the description of my J2EE component
This is the display name of my J2EE component
ProgressUploadServlet
com.test.hello.ProgressUploadServlet
PostServlet
/servlet/PostServlet
test
/servlet/test
UploadServlet
/servlet/UploadServlet
ProgressUploadServlet
/servlet/ProgressUploadServlet
index.jsp
View Code

运行结果:

  

转载于:https://my.oschina.net/u/204616/blog/545228

你可能感兴趣的文章
(二)Linux实操之——网络配置、进程管理、服务管理、组管理、YUM
查看>>
JavaScript学习笔记
查看>>
easyui使用介绍
查看>>
Java数据结构和算法(十):二叉树
查看>>
互联网金融涌动下的冲动与借债
查看>>
Android中文API(130) —— Html
查看>>
jQuery调用WCF需要注意的一些问题(转dudu)
查看>>
Sql Server2012 Reporting Services 配置管理器 (SSRS)--不支持“sharepint集成”模式选择项...
查看>>
[转载]CharSequence类型
查看>>
IOS(数据库的应用)
查看>>
biztalk 2010 映射
查看>>
hdu 2144(LCS+并查集)
查看>>
JavaScript中逗号运算符
查看>>
盘点武汉十大宜居地段
查看>>
LeetCode题解:Rotate List
查看>>
进程关系之孤儿进程组
查看>>
Foundation框架—字符串
查看>>
数据库设计中的14个技巧
查看>>
7月份文章回顾
查看>>
25个顶级的jQuery表格插件
查看>>