书签 分享 收藏 举报 版权申诉 / 18

类型java使用FileUpload实现有进度条的文件上传功能.docx

  • 文档编号:24050069
  • 上传时间:2023-05-23
  • 格式:DOCX
  • 页数:18
  • 大小:93.85KB

#728820;height:

50px;width:

0">

  

Servlet:

AjaxServlet.java

packagecom.hongfei.servlet;

importjava.io.IOException;

importjava.io.PrintWriter;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importcom.hongfei.entity.Upload;

@SuppressWarnings("serial")

publicclassAjaxServletextendsHttpServlet{

/**

*Constructoroftheobject.

*/

publicAjaxServlet(){

super();

}

/**

*Destructionoftheservlet.

*/

publicvoiddestroy(){

super.destroy();//Justputs"destroy"stringinlog

//Putyourcodehere

}

/**

*ThedoGetmethodoftheservlet.

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.

*

*@paramrequesttherequestsendbytheclienttotheserver

*@paramresponsetheresponsesendbytheservertotheclient

*@throwsServletExceptionifanerroroccurred

*@throwsIOExceptionifanerroroccurred

*/

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

response.setHeader("Cache-Control","no-store");//禁止浏览器缓存

response.setHeader("Pragrma","no-cache");//禁止浏览器缓存

response.setDateHeader("Expires",0);//禁止浏览器缓存

response.setContentType("text/html");

PrintWriterout=response.getWriter();

Uploadupload=null;

upload=(Upload)request.getSession().getAttribute("upload");

if(null==upload)

return;

longcurrentTime=System.currentTimeMillis();

//计算已用时,以S为单位

longtime=(currentTime-upload.getStartTime())/1000+1;

//计算速度,以kb为单位

longspeed=(long)(double)upload.getUploadSize()/1024/time;

//计算百分比

intpercent=(int)((double)upload.getUploadSize()/(double)upload.getTotalSize()*100);

//已经完成

intmb=(int)upload.getUploadSize()/1024/1024;

//总共有多少

inttotalMb=(int)upload.getTotalSize()/1024/1024;

//剩余时间

intshenYu=(int)((upload.getTotalSize()-upload.getUploadSize())/1024/speed);

Stringstr=time+"-"+speed+"-"+percent+"-"+mb+"-"+totalMb+"-"+shenYu;

out.print(str);

out.flush();

out.close();

}

/**

*ThedoPostmethodoftheservlet.

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.

*

*@paramrequesttherequestsendbytheclienttotheserver

*@paramresponsetheresponsesendbytheservertotheclient

*@throwsServletExceptionifanerroroccurred

*@throwsIOExceptionifanerroroccurred

*/

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

response.setContentType("text/html");

PrintWriterout=response.getWriter();

out

.println("

DOCTYPEHTMLPUBLIC\"-//W3C//DTDHTML4.01Transitional//EN\">");

out.println("");

out.println("AServlet");

out.println("");

out.print("Thisis");

out.print(this.getClass());

out.println(",usingthePOSTmethod");

out.println("");

out.println("");

out.flush();

out.close();

}

/**

*Initializationoftheservlet.

*

*@throwsServletExceptionifanerroroccurs

*/

publicvoidinit()throwsServletException{

//Putyourcodehere

}

}

Upload.java

packagecom.hongfei.servlet;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.PrintWriter;

importjava.util.Iterator;

importjava.util.List;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importmons.fileupload.FileItem;

importmons.fileupload.FileUploadException;

importmons.fileupload.disk.DiskFileItemFactory;

importmons.fileupload.servlet.ServletFileUpload;

importcom.hongfei.lister.UploadLister;

@SuppressWarnings("serial")

publicclassUploadextendsHttpServlet{

/**

*Constructoroftheobject.

*/

publicUpload(){

super();

}

/**

*Destructionoftheservlet.

*/

publicvoiddestroy(){

super.destroy();//Justputs"destroy"stringinlog

//Putyourcodehere

}

/**

*ThedoGetmethodoftheservlet.

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.

*

*@paramrequesttherequestsendbytheclienttotheserver

*@paramresponsetheresponsesendbytheservertotheclient

*@throwsServletExceptionifanerroroccurred

*@throwsIOExceptionifanerroroccurred

*/

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

response.setContentType("text/html");

PrintWriterout=response.getWriter();

out

.println("

DOCTYPEHTMLPUBLIC\"-//W3C//DTDHTML4.01Transitional//EN\">");

out.println("");

out.println("AServlet");

out.println("");

out.print("Thisis");

out.print(this.getClass());

out.println(",usingtheGETmethod");

out.println("");

out.println("");

out.flush();

out.close();

}

/**

*ThedoPostmethodoftheservlet.

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.

*

*@paramrequesttherequestsendbytheclienttotheserver

*@paramresponsetheresponsesendbytheservertotheclient

*@throwsServletExceptionifanerroroccurred

*@throwsIOExceptionifanerroroccurred

*/

@SuppressWarnings({"deprecation","unchecked"})

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

response.setContentType("text/html");

PrintWriterout=response.getWriter();

com.hongfei.entity.Uploadupload=newcom.hongfei.entity.Upload();

UploadListerlister=newUploadLister(upload);

ServletFileUploadservletFileUpload=newServletFileUpload(newDiskFileItemFactory());

//只是上传监听器

servletFileUpload.setProgressListener(lister);

request.getSession().setAttribute("upload",upload);

Listlist=null;

try{

list=servletFileUpload.parseRequest(request);

}catch(FileUploadExceptione){

e.printStackTrace();

}

for(Iteratoriter=list.iterator();iter.hasNext();){

//得到文件对象

FileItemfileItem=(FileItem)iter.next();

//是表单才进行处理

if(fileItem.isFormField()){

break;

}

//同意linux和windows的路径分隔符

Stringname=fileItem.getName().replaceAll("/","\\");

//得到文件名

intindex=name.lastIndexOf("\\");

StringfileFileName="";

if(index==-1){

fileFileName=name;

}else{

fileFileName=name.substring(index+1);

}

InputStreamfileInputStream=fileItem.getInputStream();

Stringpath=request.getRealPath("/upload");

//也可不用自己写实现方法直接使用,fileItem.write(uploadFile);

FileuploadFile=newFile(path,fileFileName);

//首先要确认路径是否存在

uploadFile.getParentFile().mkdirs();

//检查文件是否已经存在

if(!

uploadFile.exists()){

//建立文件

uploadFile.createNewFile();

}

FileOutputStreamout2=newFileOutputStream(uploadFile);

//开始copy文件

@SuppressWarnings("unused")

intlen=0;//每次读取的字节数

byte[]bytes=newbyte[1024];

while((len=fileInputStream.read(bytes,0,bytes.length))!

=-1){

out2.write(bytes);

}

out2.flush();

out2.close();

fileInputStream.close();

}

out.flush();

out.close();

}

配套讲稿:

如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

特殊限制:

部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

关 键  词:
java 使用 FileUpload 实现 进度条 文件 上传 功能
提示  冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:java使用FileUpload实现有进度条的文件上传功能.docx
链接地址:https://www.bdocx.com/doc/24050069.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2022 冰点文档网站版权所有

经营许可证编号:鄂ICP备2022015515号-1

收起
展开