当前位置: 首页 > news >正文

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类对正则验证的支持


http://www.xdnf.cn/news/819739.html

相关文章:

  • 数据库是什么
  • 网页三剑客
  • 一步一步指导 车机的备份 掌迅92XX系列固件导出步骤
  • codejock中托盘的实现
  • 一步一步搭建11gR2 rac+dg之安装rac出现问题解决(六)
  • Android 初始化Setup Wizard——Provision
  • 软考 | 2012年上半年 软件设计师 下午试卷
  • 个人博客论坛系统测试报告
  • 高等数学基础篇(数二)之二重积分(解析版)
  • Zero Sum 和为零
  • 【AI】2114- 汇总 11 个 AI 领域的优质飞书知识库,完全免费,赶紧收藏起来!
  • 【高校主办!国际高级别组委加入!EIScopus检索】2024年经济决策与人工智能国际学术会议 (EDAI 2024)
  • 什么是a站、b站、c站、d站、e站、f站、g站、h站、i站、j站、k站、l站、m站、n站?00后的世界我不懂!
  • centos7下升级openssh9.6版本以上远程连接报ssh服务器不能连接错误
  • Windows运行命令大全
  • 这儿有你所需要的所有关于科研的网址
  • Linux 命令chmod(文件管理)
  • Lightbox JS v2.0
  • 百度笔试题面试题集总
  • checklistbox控件用法总结
  • 软件外包公司对比:海辉、软通、东软集团等
  • surfacecreated啥时被调用_小豪爱上JVM--运行时数据区 | 面试必问
  • Macbook air(2012 MID)乞丐版升级——继续发挥余热
  • visio2002无法安装(您必须首先安装Enterprise Architect Edition of visual Studio.Net2003)的解决方案
  • 分享54个ASP.NET源码总有一个是你想要的
  • Web前端-BOM之Navigator对象
  • Linux内核中内存相关的操作函数
  • 由于找不到d3dx9_26.dll文件导致游戏软件无法运行启动问题
  • 《Microsoft Visual Studio 6.0 Enterprise Edition》(完整9CD,带中文MSDN Library)下载
  • Qt学习笔记之--Qt内置图标一览表