使用Smartupload组件
如果要想上传文件,则必须使用上传文件的控件框,类型为file。 1
如果要想上传文件,则必须使用上传文件的控件框,类型为file。
<formaction="SmartUpload01.jsp"method="post"> 上传文件:<inputtype="file"name="pic"> <inputtype="submit"value="上传"> </form> |
使用Smartupload上传文件,则须按照以下的步骤操作:
l实例化Smartupload对象SmartUploadsmart=newSmartUpload();
l初始化上传操作publicfinalvoidinitialize(PageContextpageContext)
作用:执行上传下载的初始化工作,其中,pageContext为JSP页面内置对象(页面上下文)
l准备上传publicvoidupload()
作用:上传文件数据。对于上传操作,第一步执行initialize方法,第二步就要执行这个方法。
l将上传的文件进行保存publicintsave(StringdestPathName)
作用:将全部上传文件保存到指定目录下,并返回保存的文件个数。其中,destPathName为文件保存目录
实例一:对于图片,必须进行多媒体的封装
<formaction="smartupload02.jsp"method="post"enctype="multipart/form-data"> 上传的图片:<inputtype="file"name="pic"> <inputtype="submit"value="上传"> </form> <%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%> <jsp:useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/> <html> <head> <title>smartupload</title> </head> <body> <% smartupload.initialize(pageContext); //初始化上传 smartupload.upload(); //准备上传 smartupload.save("upload"); //保存文件 %> </body> </html> |
实例二:封装表单的问题
<html> <head> <title>smartupload</title> </head> <body> <formaction="smartupload03.jsp"method="post"enctype="multipart/form-data"> 用户姓名:<inputtype="text"name="uname"><br> 上传的图片:<inputtype="file"name="pic"><br> <inputtype="submit"value="上传"> </form> </body> </html> <%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%> <jsp:useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/> <html> <head> <title>smartupload</title> </head> <body> 运行程序发现,无法直接使用request对象,取得表单中的其他参数 用户名不能取出为null 所以:如果表单被封装之后,要取得封装表单里的内容,则只能使用smartupload中提供的特定方法完成,而且此方法必须在smartupload准备上传的语句之后。 publicRequestgetRequest() 作用:取得request对象,以便由此对象获得上传表单参数的值。 <% request.setCharacterEncoding("GBK"); //Stringname=request.getParameter("uname"); smartupload.initialize(pageContext); //初始化上传 smartupload.upload(); //准备上传 Stringname=smartupload.getRequest().getParameter("uname"); smartupload.save("upload"); //保存文件 %> <h1>姓名:<%=name%></h1> </body> </html> |
实例三:重新命名文件名称
取得文件后缀可使用如下形式:Stringext=smart.getFiles().getFile(0).getFileExt(); 1.publicFilesgetFiles() 作用:取全部上传文件,以Files对象形式返回,可以利用Files类的操作方法来获得上传文件的数目等信息。2.Files类中的方法:publicFilegetFile(intindex) 作用:取得指定位移处的文件对象File(这是smartupload中的File,不是java.io.File,注意区分)其中,index为指定位移,其值在0到getCount()-1之间。 3.File类中的方法:publicStringgetFileExt() 作用:取文件扩展名(后缀) 作用:将文件换名另存,其中,destFilePathName是另存的文件名。 |
<body> <formaction="smartupload04.jsp"method="post"enctype="multipart/form-data"> 文件名称:<inputtype="text"name="uname"><br> 上传的图片:<inputtype="file"name="pic"><br> <inputtype="submit"value="上传"> </form> </body> <%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%> <jsp:useBeanid="smart"class="org.lxh.smart.SmartUpload"/> <html> <head> <title>smartupload</title> </head> <body> <% request.setCharacterEncoding("GBK"); smart.initialize(pageContext); //初始化上传 smart.upload(); //准备上传 //取得上传的文件后缀 Stringext=smart.getFiles().getFile(0).getFileExt(); //取得用户自己输入的名称: Stringname=smart.getRequest().getParameter("uname"); //将用户名与扩展名相连接,形成新的名称 name=name+"."+ext; //新文件的路径及文件名 StringfileName=this.getServletContext().getRealPath("/")+"upload/"+name; //保存文件 smart.getFiles().getFile(0).saveAs(fileName); %> <imgsrc="<%=fileName%>"width="300"height="200"> </body> </html> |
实例四:为保证重命名不重复
smartupload05.htm/smartupload05.jsp/IPTimeStamp.java
<html> <head> <title>smartupload</title> </head> <body> <formaction="smartupload05.jsp"method="post"enctype="multipart/form-data"> 上传的图片:<inputtype="file"name="pic"><br> <inputtype="submit"value="上传"> </form> </body> </html> <%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%> <%@pageimport="com.bean.*"%> <jsp:useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/> <html> <head> <title>smartupload</title> </head> <body> <% IPTimeStampits=newIPTimeStamp(request.getRemoteAddr()); request.setCharacterEncoding("GBK"); smartupload.initialize(pageContext); //初始化上传 smartupload.upload(); //准备上传 Stringname=its.getIPTimeStampRand()+"."+smartupload.getFiles().getFile(0).getFileExt(); StringfileName=this.getServletContext().getRealPath("/")+"upload/"+name; smartupload.getFiles().getFile(0).saveAs(fileName); %> <imgsrc="<%=fileName%>"width="300"height="200"> </body> </html> packagecom.bean; importjava.text.SimpleDateFormat; importjava.util.Random; publicclassIPTimeStamp{ privateStringip; publicIPTimeStamp(){ } publicIPTimeStamp(Stringip){ this.ip=ip;//设置ip地址 } publicStringgetTimeStamp(){ //取得日期和时间 Stringtemp=null; SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMddHHmmssSSS"); temp=sdf.format(newjava.util.Date()); returntemp; } publicStringgetIPTimeStampRand(){ StringBufferbuf=newStringBuffer(); if(ip!=null){ Stringstr[]=this.ip.split("\\.");//以点为他隔符划分ip for(inti=0;i<str.length;i++){ //将补全3位的IP地址放入buf buf.append(this.addZero(str[i],3)); } } //追加日期及时间 buf.append(this.getTimeStamp()); //追加产生的3位随机数 Randomrand=newRandom(); for(inti=0;i<3;i++){ buf.append(rand.nextInt(10)); } returnbuf.toString(); } privateStringaddZero(Stringstr,intlen){ //ip地址没有写全3位时,填0补全 StringBuffers=newStringBuffer(); s.append(str); while(s.length()<len){ s.insert(0,"0"); } returns.toString(); } } |
运行结果