smartupload文件上传技术

本文详细介绍SmartUpload组件的使用方法,包括如何配置、初始化、上传文件等,并提供了自动重命名文件以避免覆盖的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SmartUpload 

1、smartUpload是由www.jspsmart.com 网站开发的一套上传组件包,可以轻松实现上传及下载功能,smartUpload组件使用简单、可以轻松的实现上传文件类型的限制、也可以轻易取得上传文件的名称、后缀、大小等

2、smartUpload本身是一个系统提供的jar包,用户直接将此包放到classpath下即可,也可以直接拷贝到TOMCAT_HOME\lib 目录之中

3、之所以现在继续讲解此组件的使用,原因是此组件比较简单,特别是针对JSP开发的时候最能体现出来

4、smartUpload 给出的时候是以一个jar的形式给出的,所有要想使用,则必须首先在lib 目录中进行配置

5、虽然使用此组件可以完成上传,但是上传的时候有一点要注意,必须选择好要上传的文件是哪一个,这个选择肯定是HTML中的表单元素完成。

6、由于现在要上传文件了,则表单必须进行封装,因为传递的不再是普通的文本数据,那么表单封装要通过enctype (二进制文件)完成。

[html]  view plain  copy
 print ?
  1. <html>  
  2. <head> <title>欢迎光临</title>  
  3. </head>  
  4. <body>  
  5. <form action="smartupload.jsp" method="post" enctype="multipart/form-data">  
  6.     请输入上传文件: <input type="file" name="pic">  
  7.     <input type="submit" value="上传">  
  8. </form>  
  9. </body>  
  10. </html>  

         现在既然表单已经完成,则此时就可以通过smartUpload完成上传,但是在上传之前先建立一个upload的文件夹,这个文件夹保存在web根目录下

建立完成之后,如果要想进行上传操作的话,则必须按照如下的步骤进行

1、实例化SmartUpload 对象

2、初始化上传的步骤

3、准备上传

4、保存文件

[html]  view plain  copy
 print ?
  1. <%@ page contentType="text/html" pageEncoding="GBK"%>、  
  2. <%@ page import="org.lxh.smart.*"%>  
  3. <html>  
  4. <head> <title>欢迎光临</title>  
  5. </head>  
  6. <body>  
  7. <%  
  8.     SmartUpload smart = new SmartUpload();  
  9.     smart.initialize(pageContext);  //初始化上传操作  
  10.     smart.upload();         //上传准备  
  11.     smart.save("upload");   //存入指定保存文件  
  12. %>  
  13. </body>  
  14. </html>  

   此时已经完成上传,而且最大的特点可以发现,原本的文件名称就是上传之后的文件名称,没有发生任何的改变。

但是,这个时候有一个新的问题出现了,如果要进行文件上传,则肯定要对表单进行封装,单是表单一旦封装之后,则就无法使用 request.getParatermeter() 接受参数了

[html]  view plain  copy
 print ?
  1. <%@ page contentType="text/html" pageEncoding="GBK"%>、  
  2. <%@ page import="org.lxh.smart.*"%>  
  3. <html>  
  4. <head> <title>欢迎光临</title>  
  5. </head>  
  6. <body>  
  7. <%  
  8.     request.setCharacterEncoding("GBK");  
  9. %>  
  10. <%  
  11.     SmartUpload smart = new SmartUpload();  
  12.     smart.initialize(pageContext);  //初始化上传操作  
  13.     smart.upload();         //上传准备  
  14.     smart.save("upload");   //存入指定保存文件  
  15. %>  
  16. <h2><%=request.getParameter("uname")%></h2>  
  17. </body>  
  18. </html>  

      现在根本无法接受参数,而且使用getParameter()也无法接收,因为表单被封装了,所有的数据不再是文本了,而是(enctype="multipart/form-data")二进制的byte 流了,那么这个时候要想接收,则必须使用SmartUpload中提供的方法支持。方法如下:

[html]  view plain  copy
 print ?
  1. <%@ page contentType="text/html" pageEncoding="GBK"%>、  
  2. <%@ page import="org.lxh.smart.*"%>  
  3. <html>  
  4. <head> <title>欢迎光临</title>  
  5. </head>  
  6. <body>  
  7. <%  
  8.     request.setCharacterEncoding("GBK");  
  9. %>  
  10. <%  
  11.     SmartUpload smart = new SmartUpload();  
  12.     smart.initialize(pageContext);  //初始化上传操作  
  13.     smart.upload();         //上传准备  
  14.     String name = smart.getRequest().getParameter("uname");  
  15.     smart.save("upload");   //存入指定保存文件  
  16. %>  
  17. <h2><%=request.getParameter("uname")%></h2>  
  18. <h2>名称: <%=name%></h2>  
  19. </body>  
  20. </html>  

     现在确实解决了文件的上传的操作,但是一个新的问题又出现了,这个问题就是本身会出现覆盖的情况,因为文件名称相同,所有会覆盖。

那么应该如何解决文件覆盖的问题呢?

为文件上传自动命名

   如果多个用户上传的文件的名称一样,则肯定会发生覆盖的情况,为了解决这种问题,可以采用为上传文件自动命名的方式,为了防止重名,自动命名可以采用如下的格式:

1、IP地址+时间戳+三位随机数

2、例如: 现在练级的IP地址是:192.168.2.3 ; 日期时间是: 2011-11-1 01:41:24.043 ; 三位随机数是:200, 则拼凑出的新文件名称就是:

19216800200320111101014124043200.文件后缀

下面将该类封装:

代码如下:

package webstudy;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class IpTimeStamp {
   private SimpleDateFormat sdf = null;
   private String ip = null;
   public IpTimeStamp(){
	   
   }
   public IpTimeStamp(String ip){
	   this.ip=ip;
   }
   public String getIpTimeRand(){
	   StringBuffer buf  = new StringBuffer();
	   if(this.ip!=null){
		   String s[] = this.ip.split("\\.");
		   for(int i=0;i<s.length;i++){
			   buf.append(this.addZero(s[i],3));
		   }
	   }
	       buf.append(this.getTimeStamp());
		   Random r= new Random();
		   for(int i=0;i<3;i++){
			   buf.append(r.nextInt(10));
		   }   
	  return buf.toString();
   }
   public String getDate(){
	   this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	   return this.sdf.format(new Date());
   }
   public String getTimeStamp(){
	   this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	   return this.sdf.format(new Date());
   }
   private String addZero(String str,int len){
	   StringBuffer s = new StringBuffer();
	   s.append(str);
	   while(s.length()<len){
		   s.insert(0, "0");
	   }
	   return s.toString();
   }
}

下面通过一个例子实现:

smartupload.html:

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="smartupload_demo" method="post" 
 enctype="multipart/form-data">
请选择要上传的文件:<input type="file" name="pic"><br>
请选择要上传的文件:<input type="file" name="pic"><br>
请选择要上传的文件:<input type="file" name="pic"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
servlet:   

smartupload_demo的主要代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("gbk");
		 SmartUpload smart= new SmartUpload();
		  smart.initialize(this.getServletConfig(),request,response);//初始化上传操作
		  
		  try {
			smart.upload();
			//IpTimeStamp its =new IpTimeStamp(request.getRemoteAddr());
			//取得客户端的ip地址
			IpTimeStamp its =new IpTimeStamp("123.234.12.23");
			for(int i=0;i<smart.getFiles().getCount();i++){
				System.out.print("--------------"+smart.getFiles().getCount());
			String ext = smart.getFiles().getFile(i).getFileExt();//扩展名称
			String fileName = its.getIpTimeRand()+"."+ext;
			smart.getFiles().getFile(i).saveAs(this.getServletContext().getRealPath("/")
					+"upload"+java.io.File.separator+fileName);
			// smart.save("D:/upload");
			}
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //上传准备
		  //String name=smart.getRequest().getParmeter("uname");
		 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值