JSP实现快速上传文件的方法
本文实例讲述了JSP实现快速上传文件的方法。分享给大家供大家参考。具体如下:
这里演示JSP不使用第三方库,实现快速上传文件的功能
1.FileUpload.java:
packageFileUpload; importjava.io.BufferedOutputStream; importjava.io.File; importjava.io.FileOutputStream; importjavax.servlet.ServletInputStream; /** * */ /** *@authorQch * */ publicclassFileUpload { ServletInputStreamin=null; Stringfpath="C://"; publicFileUpload() { fpath="C://"; in=null; } publicvoidsetInputStream(ServletInputStreamin) { this.in=in; } publicvoidsetFpath(Stringp) { this.fpath=p; } publicStringgetFpath() { returnfpath; } publicStringgetParameter() { Stringr=null; try { r=getParameter(in); } catch(Exceptione) { e.printStackTrace(); } returnr; } publiclonggetFileUpload() { longr=-1; try { r=getFileUpload(in,fpath); } catch(Exceptione) { e.printStackTrace(); } returnr; } publicStringgetParameter(ServletInputStreamin)//只能按顺序提取 throwsException { intl=0; byte[]b=newbyte[1024]; l=in.readLine(b,0,b.length);//依次是读取属性的开始符、名称、属性值的类型、属性的值 Stringsi=newString(b); if(si.startsWith("----------------------------")) {//表示是从开始符开始读,否则应为刚读取文件后的一个属性,此时应少读一次 l=in.readLine(b,0,b.length); } l=in.readLine(b,0,b.length); l=in.readLine(b,0,b.length); Stringvalue=newString(b,0,l); returnvalue; } publiclonggetFileUpload(ServletInputStreamin,Stringfpath)//需要提供输入流和存储路径 throwsException { //out.println("文件信息:<br>"); longbegin=System.currentTimeMillis();//传送时间计时开始 intl=0; byte[]b=newbyte[1024]; l=in.readLine(b,0,b.length); Stringsign=newString(b,0,l);//eg.-----------------------------7d9dd29630a34 l=in.readLine(b,0,b.length); Stringinfo=newString(b,0,l);//eg.Content-Disposition:form-data; //name="file"; l=in.readLine(b,0,b.length); //Stringtype=new //String(b,0,l);//eg.Content-Type:application/octet-stream(程序文件) l=in.readLine(b,0,b.length); //Stringnulll=newString(b,0,l);//此值应为空 intnIndex=info.toLowerCase().indexOf("filename=\""); intnLastIndex=info.toLowerCase().indexOf("\"",nIndex+10); Stringfilepath=info.substring(nIndex+10,nLastIndex); intna=filepath.lastIndexOf("\\"); Stringfilename=filepath.substring(na+1); //out.println("文件绝对路径:"+filepath+"<br>"); //out.println("文件名:"+filename+"<br><br>"); Stringpath=fpath+filename; Filefi=newFile(path);//建立目标文件 if(!fi.exists()&&!fi.createNewFile()) return-2; BufferedOutputStreamf=newBufferedOutputStream(newFileOutputStream( fi)); while((l=in.readLine(b,0,b.length))>0) { if(l==sign.length()) { Stringsign1=newString(b,0,sign.length()); //out.println(sign1+"<br>"); if(sign1.startsWith(sign))//比对是否文件已传完 break; } f.write(b,0,l); f.flush(); } f.flush(); f.close(); longend=System.currentTimeMillis();//传送时间计时结束 //out.println("上传文件用时:"+(end-begin)+"毫秒<br>"); returnend-begin; } }
2.submitFile.jsp:
<%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%> <% Stringpath=request.getContextPath(); StringbasePath=request.getScheme()+"://" +request.getServerName()+":"+request.getServerPort() +path+"/"; %> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>MyJSP'submitFile.jsp'startingpage</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="Thisismypage"> <!-- <linkrel="stylesheet"type="text/css"href="styles.css"> --> <scriptlanguage="javascript"> functioncheck() { if(document.form2.name.value==""){ alert("姓名不能为空!!"); document.form2.name.focus(); returnfalse; } if(document.form2.file.value==""){ alert("文件不能为空!!"); returnfalse; } returntrue; } </script> </head> <body> <br> <formmethod="post"name="form2"enctype="MULTIPART/FORM-DATA" action="AnswerFile.jsp"> <br> <palign="center"> <br> </p> <tablewidth="530"border="1"bgcolor="#c0c0c0"align="center" height="91"> <tbody> <tr> <tdvalign="top"align="right"> 姓名 <br> </td> <tdvalign="top"> <inputtype="text"name="name"> </td> </tr> <tr> <tdalign="right"> 文件 </td> <tdalign="left"> <inputtype="file"name="file"> </td> </tr> <tr> <tdvalign="top"align="right"> 文件类型 <br> </td> <tdvalign="top"align="left"> <selectsize="1"name="leixing"> <optionselectedvalue="作业"> 作业 </option> <optionvalue="课程设计"> 课程设计 </option> <optionvalue="论文"> 论文 </option> </select> </td> </tr> <tr> <tdalign="right"> <inputtype="Submit"value="上传"name="button2"onclick="return(check());"> </td> <tdalign="left"> <inputtype="reset"value="重置"name="button3"> </td> </tr> </tbody> </table> <p> <br> <br> </p> </form> </body> </html>
3.AnswerFile.jsp:
<%@pagelanguage="java"import="java.util.*,java.io.*" pageEncoding="GB18030"%> <% Stringpath=request.getContextPath(); StringbasePath=request.getScheme()+"://" +request.getServerName()+":"+request.getServerPort() +path+"/"; %> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>MyJSP'AnswerFile.jsp'startingpage</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="Thisismypage"> <!-- <linkrel="stylesheet"type="text/css"href="styles.css"> --> </head> <body> <jsp:useBeanid="upload"scope="session"class="FileUpload.FileUpload"/> <jsp:setPropertyname="upload"value="C://"property="fpath"/> <% ServletInputStreamin=request.getInputStream(); upload.setInputStream(in); Stringnam=upload.getParameter(); out.println("姓名:"+nam+"<br><br>"); longtime=upload.getFileUpload(); out.println("文件上传完毕,总共耗时:"+time+"毫秒<br>"); Stringleixing=upload.getParameter(); out.println("文件类型:"+leixing+"<br>"); in.close(); %> <br> <divalign="right"> <ahref="index.jsp">回到首页</a> </div> </body> </html>
希望本文所述对大家的JSP程序设计有所帮助。