SmartUpload.docx
- 文档编号:17691375
- 上传时间:2023-08-03
- 格式:DOCX
- 页数:17
- 大小:627.47KB
SmartUpload.docx
《SmartUpload.docx》由会员分享,可在线阅读,更多相关《SmartUpload.docx(17页珍藏版)》请在冰点文库上搜索。
SmartUpload
----作者:
张小龙email:
zxlxgd@
SmartUpload的作用
1.SmartUpload的主要作用:
主要是完成上传操作,如果想要上传的话,可以直接使用html表单中的file元素即可。
常见的上传组件:
Smartupload,ApacheFileUpload
2.使用Smartupload上传文件
编写一个表单,表单中使用file元素。
用户姓名:
上传的图片:
之后编写jsp文件,接收上传的内容,上传的步骤分为以下几个步骤:
1.初始化上下文
2.准备上传
3.保存文件
范例:
Smartupload01.jsp:
<%@pagelanguage="java"import="java.util.*"pageEncoding="gbk"%>
useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/>
<%
request.setCharacterEncoding("gbk");
smartupload.initialize(pageContext);//初始化上传
smartupload.upload();//准备上传
Stringname=smartupload.getRequest().getParameter("uname");
smartupload.save("upload");//保存文件
%>
<%=name%>
注意:
上传文件的时候表单需要封装。
如果不封装,则找不到上传的文件。
4.封装表单
在form元素中有一个enctype的属性,multipart/form-data:
窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,上传附件用到
即enctype=”multipart/form-data”
5.表单封装后request无法取得参数了。
范例:
SmartUpload02.jsp
Stringname=smartupload.getRequest().getParameter("uname");
表单被封装后里面传递的不再是文本内容了。
所以无法通过getParameter()方法取得。
但是可以依靠smartupload对象完成,但是在使用前,必须保证smartupload以及初始化完成。
6.为上传的文件重命名
文件可能存在后缀,在修改文件名时,不应该修改其后缀名。
范例:
Smartupload04.jsp:
文件名称:
上传的图片:
那么之后根据完整的文件名称拼凑一个文件名称出来。
<%@pagelanguage="java"import="java.util.*"pageEncoding="gbk"%>
useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/>
<%
request.setCharacterEncoding("gbk");
smartupload.initialize(pageContext);//初始化上传
smartupload.upload();//准备上传
Stringname=smartupload.getRequest().getParameter("uname");
name=name+"."+smartupload.getFiles().getFile(0).getFileExt();//得到文件的扩展名
Stringfilename=this.getServletContext().getRealPath("/")+"upload/"+name;
smartupload.getFiles().getFile(0).saveAs(filename);
%>
<%=name%>
上传图片并且在另一个页面显示图片:
<%@pagelanguage="java"import="java.util.*"pageEncoding="gbk"%>
useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/>
<%
request.setCharacterEncoding("gbk");
smartupload.initialize(pageContext);//初始化上传
smartupload.upload();//准备上传
Stringname=smartupload.getRequest().getParameter("uname");
name=name+"."+smartupload.getFiles().getFile(0).getFileExt();//得到文件的扩展名
Stringfilename=this.getServletContext().getRealPath("\\")+"upload\\"+name;
System.out.println(filename);
smartupload.getFiles().getFile(0).saveAs(filename);
%>
<%=name%>
"width="200"height="300">
10.为上传文件自动命名:
如果想要自动命名,则要想怎么命名才可以不重复?
·可以采用以下方式命名:
·ip地址+时间戳+三位随即数
·例如:
|-IP:
192.168.0.23
|-时间戳:
2011-05-1321:
38:
39123
|-生成的最后结果:
19216802320110513213839123+后缀
单独编写javabean方便以后调用。
此javabean负责此文件的前缀。
范例:
IPTimeRand.java
packageorg.zxl.util;
importjava.text.SimpleDateFormat;
importjava.util.Random;
publicclassIPTimeRand{
privateStringip;
publicStringgetIPTimeRand(){
StringBufferbuf=newStringBuffer();
if(buf!
=null){
Stringstr[]=ip.split("\\.");
for(inti=0;i buf.append(this.addZero(str[i],3)); } } buf.append(getTimeStamp()); Randomrand=newRandom(); for(inti=0;i<3;i++){ buf.append(rand.nextInt(10)); } returnbuf.toString(); } publicIPTimeRand(Stringip){ this.ip=ip; } publicStringgetTimeStamp(){ Stringtemp=null; SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMddHHmmssSSS"); temp=sdf.format(newjava.util.Date()); returntemp; } publicStringaddZero(Stringstr,intlen){ StringBuffers=newStringBuffer(); s.append(str); while(s.length() s.insert(0,"0"); } returns.toString(); } publicstaticvoidmain(Stringargs[]){ System.out.println(newIPTimeRand("192.168.1.1").getIPTimeRand()); } } SmartUpload05.html:
上传的图片:
SmartUpload05.jsp:
<%@pagelanguage="java"import="java.util.*"pageEncoding="gbk"%>
useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/>
<%
request.setCharacterEncoding("gbk");
smartupload.initialize(pageContext);//初始化上传
smartupload.upload();//准备上传
Stringname=smartupload.getRequest().getParameter("uname");
name=name+"."+smartupload.getFiles().getFile(0).getFileExt();//得到文件的扩展名
Stringfilename=this.getServletContext().getRealPath("\\")+"upload\\"+name;
System.out.println(filename);
smartupload.getFiles().getFile(0).saveAs(filename);
%>
<%=name%>
"width="200"height="300">
11.上传说明:
一般在系统开发的时候要将一些图片信息保存在数据库中,但是此种做法有两种:
1.直接在数据库中存储图片信息,通过BLOG字段存储
·文件较大的话,此方法不方便
·数据库备份,只备份数据库就好了
2.将一个图片上传的一个文件夹中,之后再数据库中记录好文件的路径
·内容大,此方法方便
·数据库备份,还要保存文件夹中的图片
向数据库中保存信息
使用上传路径的方法保存:
1.现在又如下数据库创建脚本:
dropsequencemyseq;
droptableperson;
createsequencemyseq;
createtableperson(
idnumberprimarykeynotnull,
namevarchar2(60)notnull,
photovarchar2(100)notnull
);
2.insert.html
姓名:
上传的图片:
3.insert.jsp:
<%@pagecontentType="text/html;charset=gbk"%>
<%@pageimport="org.zxl.util.*"%>
<%@pageimport="java.sql.*"%>
useBeanid="dbc"class="org.zxl.demo.DataBaseConnection"/> useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/>
<%
try{
request.setCharacterEncoding("gbk");
smartupload.initialize(pageContext);//初始化上传
smartupload.upload();//开始上传
IPTimeRandipt=newIPTimeRand(request.getRemoteAddr());
Stringphotoname=ipt.getIPTimeRand()+"."+smartupload.getFiles().getFile(0).getFileExt();
Stringsql="insertintoperson(id,username,photo)values(myseq.nextVal,?
?
)";//数据库执行语句
PreparedStatementpstm=dbc.getConnection().prepareStatement(sql);
pstm.setString(1,smartupload.getRequest().getParameter("username"));
pstm.setString(2,photoname);
pstm.executeUpdate();
pstm.close();
Stringfilename=this.getServletContext().getRealPath("/")+"upload/"+photoname;
smartupload.getFiles().getFile(0).saveAs(filename);
}catch(Exceptione){
}finally{
dbc.close();
%>
forwardpage="display.jsp"/> <% } %> 4.display.jsp: <%@pagecontentType="text/html;charset=gbk"%> <%@pageimport="org.zxl.util.*"%> <%@pageimport="java.sql.*"%> useBeanid="smartupload"class="org.lxh.smart.SmartUpload"/> useBeanid="dbc"class="org.zxl.demo.DataBaseConnection"/>
<%
try{
request.setCharacterEncoding("gbk");
Stringsql="selectid,username,photofromperson";
PreparedStatementprst=dbc.getConnection().prepareStatement(sql);
ResultSetrs=prst.executeQuery();
%>
<%
while(rs.next()){
intid=rs.getInt
(1);
Stringname=rs.getString
(2);
Stringphoto=rs.getString(3);
StringfileName=this.getServletContext().getRealPath("/")+"upload/"+photo;
%>
<%
}
%>
<%
}catch(Exceptione){
}finally{
dbc.close();
}
%>
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SmartUpload