当前位置:编程学习 > JAVA >>

Servlet:htm+javascript+css+servlet (ajax)实现上传(能显示进度条)

src/UploadStatus.java ,数据模型类
[java]  
package com.xieyuan;  
  
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;  
    }  
  
}  
 
src/UploadListener.java,继承自ProgressListener,当使用commons_uploadfile组件上传时,调用组件类的方法可添加上传监听,组件就会不断调用UploadListener的update方法
[java] view plaincopy
package com.xieyuan;  
  
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);  
          
    }  
}  
 
src/UploadServlet.java,该类doPost方法用于处理上传,index.jsp后台会使用XmlHttpRequest调用本Servlet的doGet方法,从session中获取最新的上传数据情况
[java] 
package com.xieyuan;  
  
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.List;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.sound.sampled.AudioFormat.Encoding;  
  
import org.apache.commons.fileupload.DiskFileUpload;  
import org.apache.commons.fileupload.FileItem;  
import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  
  
public class UploadServlet extends HttpServlet {  
  
    //定义临时文件盒上传文件的存储路径  
    private File uploadTemp=null;  
    private File uploadPath=null;  
      
    /** 
     * Constructor of the object. 
     */  
    public UploadServlet() {  
        super();  
    }  
  
    /** 
     * Destruction of the servlet. <br> 
     */  
    public void destroy() {  
        super.destroy(); // Just puts "destroy" string in log  
        // Put your code here  
    }  
  
  
    /** 
     * The doGet method of the servlet. <br> 
     * 
     * 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 {  
         //禁用缓存,index.jsp后台会使用XmlHttpRequest调用本Servlet的doGet方法,从session中获取最新的上传数据情况  
        response.setHeader("Cache-Control", "no-store");  
        response.setHeader("Pragrma", "no-cache");  
        response.setDateHeader("Expires", 0);  
        response.setContentType("text/html;charset=utf-8");  
        UploadStatus status = (UploadStatus) request.getSession(true)  
                .getAttribute("uploadStatus");  
      
        if (status == null) {  
            response.getWriter().println("没有上传信息");  
              
            return;  
        }  
        long startTime = status.getStartTime();  
        long currentTime = System.current
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,