SmartUpload组件实现文件上传


调试好几天的程序,原来是软件版本的原因,我靠,能说脏话吗?好吧,当然这个过程虽然痛苦,但程序猿要学会享受同bug战斗的乐趣。

实现过程:
1、将下载好的smartupload.jar文件导入项目lib夹中

这里说明:如果不用MyEclipse工具,要注意把这个jspsmartupload.jar包用解压缩文件打开,就能看到里面的文件夹目录,让我进一步体会了导入包的概念和实质:
(解压图解)



<%@page import="com.jspsmart.upload.*"%>导入的包的实质是这些class文件和META-INF文件:


否则会出现错误:
Generated servlet error:SmartUpload cannot be resolved to a type

2、编写实现自动命名的工具类IPTimeStamp:


package zz.util;

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){	// 接收IP地址
		this.ip = ip;
    }

    public String getIPTimeRand(){	// 得到IP地址+时间戳+三位随机数
        StringBuffer buf = new StringBuffer();	// 实例化StrintBuffter对象
        if(this.ip != null) {
            String s[] = ip.split("\\.");	// 按.进行拆分
			//System.out.println(s.length);
            for(int i = 0; i < s.length; i++)
                buf.append(addZero(s[i], 3));	// 不够三位要补0

        }
        buf.append(getTimeStamp());
        Random random = new Random();	
        for(int j = 0; j < 3; j++)	{// 增加一个三位的随机数
            buf.append(random.nextInt(10));
		}
        return buf.toString();		// 返回名称
    }

    public String addZero(String str, int len){
        StringBuffer buf = new StringBuffer();
        buf.append(str);
		while(buf.length() < len){	// 如果不够指定位数要在前面补0	
			buf.insert(0, "0");		
		} 
			return buf.toString();
    }

    public String getTimeStamp(){
        sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return this.sdf.format(new Date());
    }

}

测试程序:
 public static void main(String []args){
    	System.out.println(new IPTimeStamp("172.19.34.25").getIPTimeRand());
    	
    }
上传表单upload_demo_01.htm:
<html>
<head><title>上传单个图片</title></head>
<body>
	<form action="upload_demo_03.jsp" method="post" enctype="multipart/form-data">
		姓名:<input type = "text" name = "uname"><br>
		照片:<input type="file" name ="pic"><br>
		<input type="submit" value="上传"><input type="reset" value="重置">
	</form>
</body>
</html>



增加自动命名功能upload_demo_01.htm:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="com.jspsmart.upload.*"%>
<%@ page import="zz.util.IPTimeStamp"%>

<html>
<head><title>上传自动命名的文件</title></head>
<body>
<% request.setCharacterEncoding("GBK");%>
<%
	SmartUpload smart = new SmartUpload();	// 实例化SmartUpload组件
	smart.initialize(pageContext);	// 初始化上传操作
	smart.upload();			// 上传准备
	IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr());	// 取得客户端IP地址
	String ext = smart.getFiles().getFile(0).getFileExt();		// 取得文件后缀
	String fileName = its.getIPTimeRand() + "." + ext;	// 拼凑文件名称
	smart.getFiles().getFile(0).saveAs(getServletContext().getRealPath("/")+ "upload"+java.io.File.separator + fileName);	// 保存文件
	String name = smart.getRequest().getParameter("uname");	// 接收请求参数
%>
<h2>姓名:<%=name%></h2>
<img src = "../upload/<%=fileName%>" width="180" height="240">
</body>
</html>

再次出现编译错误:

org.apache.jasper.JasperException:Exception in JSP:

javax.servlet.ServletException: File can't be saved

查了N多资料,最后一怒之下把把tomcat5.5卸载了,装了tomcat6.0,文件上传问题被KO!很汗吧……
上传表单界面:



上传后效果:




自动命名情况(其它上传的照片结果):


PS:我喜欢华仔!
为了庆祝自己经过几天KO掉程序bug,特上传本淫照片一张:




当然还有个问题: IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr());// 取得客户端IP地址
得到我的本机IP地址,只有显示前3位127,可能是浏览器防火墙设置的问题。
注意:

1、如果要进行文件上传操作,在表单处必须使用enctype将文件封闭成一个二进制数据才可以接收。

2、如何限制文件上传类型?

答:通过正则表达式

If(smart.getFiles().getFile(0).getFileName() . match(“^\\w+\\.(gif|jsp) $”)){
 
}


回头仔细看这程序,包含的信息量非常大:

1、取得IP

2、后缀名过滤

3、随机数(抽牌算法)

4、时间类

5、String类对正则验证的支持


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值