java使用FileUpload实现有进度条的文件上传功能.doc_第1页
java使用FileUpload实现有进度条的文件上传功能.doc_第2页
java使用FileUpload实现有进度条的文件上传功能.doc_第3页
java使用FileUpload实现有进度条的文件上传功能.doc_第4页
java使用FileUpload实现有进度条的文件上传功能.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

先看看效果:页面: base href= java进行文件上传,带进度条 var over = false;var inter;function upload()over = false;$(#state).html()$(#progress).css(width,0)$(inputtype=submit).attr(disabled,true);$(#progress).css(width,0%);$(#state).html(正在上传. 总大小:0MB,已上传:0MB,0%,已用时:0秒,剩余时间:0秒,速度:0KB/S);inter = setInterval(req,1000);function req()/如果上传已经完成if(over)clearInterval(inter);return;var url = upload/AjaxServlet;$.get(url,function(date)var state = date.split(-);$(#state).html(正在上传. 总大小:+state4+MB,已上传:+state3+MB,+state2+%,已用时:+state0+秒,剩余时间:+state5+秒,速度:+state1+KB/S);$(#progress).animate(width:state2+%,500);if(state3 = state4)over = true;$(inputtype=submit).attr(disabled,false);$(#state).html(上传已完成,总大小:+state4+MB,已上传:+state3+MB,+state2+%,已用时:+state0+秒,剩余时间:+state5+秒,速度:+state1+KB/S);); :AjaxServlet.javapackage com.hongfei.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.hongfei.entity.Upload;SuppressWarnings(serial)public class AjaxServlet extends HttpServlet /* * Constructor of the object. */public AjaxServlet() 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);/ 禁止浏览器缓存response.setContentType(text/html);PrintWriter out = response.getWriter();Upload upload = null;upload = (Upload)request.getSession().getAttribute(upload);if(null = upload)return;long currentTime = System.currentTimeMillis();/计算已用时,以S为单位long time = (currentTime - upload.getStartTime() / 1000 + 1;/计算速度,以kb为单位long speed = (long)(double)upload.getUploadSize() / 1024 / time;/计算百分比int percent = (int)(double)upload.getUploadSize() / (double)upload.getTotalSize() * 100);/已经完成int mb = (int)upload.getUploadSize() / 1024 / 1024;/总共有多少int totalMb = (int)upload.getTotalSize() / 1024 / 1024;/剩余时间int shenYu = (int)(upload.getTotalSize() - upload.getUploadSize() / 1024 / speed);String str = time+-+speed+-+percent+-+mb+-+totalMb+-+shenYu;out.print(str);out.flush();out.close();/* * 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 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 hereUpload.javapackage com.hongfei.servlet;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;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 mons.fileupload.FileItem;import mons.fileupload.FileUploadException;import mons.fileupload.disk.DiskFileItemFactory;import mons.fileupload.servlet.ServletFileUpload;import com.hongfei.lister.UploadLister;SuppressWarnings(serial)public class Upload extends HttpServlet /* * Constructor of the object. */public Upload() 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.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 GET method);out.println( );out.println();out.flush();out.close();/* * 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 */SuppressWarnings( deprecation, unchecked )public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(text/html);PrintWriter out = response.getWriter();com.hongfei.entity.Upload upload = new com.hongfei.entity.Upload();UploadLister lister = new UploadLister(upload);ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory();/只是上传监听器servletFileUpload.setProgressListener(lister);request.getSession().setAttribute(upload, upload);List list = null;try list = servletFileUpload.parseRequest(request); catch (FileUploadException e) e.printStackTrace();for(Iterator iter = list.iterator(); iter.hasNext();)/得到文件对象FileItem fileItem = (FileItem)iter.next();/是表单才进行处理if(fileItem.isFormField()break;/同意linux和windows的路径分隔符String name = fileItem.getName().replaceAll(/, );/得到文件名int index = name.lastIndexOf();String fileFileName = ;if(index = -1)fileFileName = name;elsefileFileName = name.substring(index + 1);InputStream fileInputStream = fileItem.getInputStream();String path = request.getRealPath(/upload);/也可不用自己写实现方法直接使用,fileItem.write(uploadFile);File uploadFile = new File(path,fileFileName);/首先要确认路径是否存在uploadFile.getParentFile().mkdirs();/检查文件是否已经存在if(!uploadFile.exists()/建立文件uploadFile.createNewFile();FileOutputStream out2 = new FileOutputStream(uploadFile);/开始copy文件SuppressWarnings(unused)int len = 0;/每次读取的字节数byte bytes = new byte1024; while(len = fileInputStream.read(bytes, 0, bytes.length) != -1)out2.write(bytes);out2.flush();out2.close();fileInputStream.close();out.flush();out.close();/* * Initialization of the servlet. * * throws ServletException if an error occurs */public void init() throws ServletException / Put your code here:监听UploadLister.javapackage com.hongfei.lister;import mons.fileupload.ProgressListener;import com.hongfei.entity.Upload;public class UploadLister implements ProgressListenerprivate Upload upload = null;public UploadLister(Upload upload)this.upload = upload

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论