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