使用 smartupload 上传文件
1、导入jar包:
2、在表单中添加表单属性:enctype=”multipart/form-data”;
<form action="doindex.jsp" enctype="multipart/form-data" method="post">
头像:<input type="file" name="ifile"/><br/>
用户名:<input type="text" name="iname"/><br/>
<input type="submit" value="提交"/>
</form>
3、逻辑处理页面:
<%
//实例化一个SmartUpload对象
SmartUpload su = new SmartUpload();
//初始化 pageContext是九大内置对象之一 作用域仅限于当前页面
su.initialize(pageContext);
//设置允许上传的文件类型
su.setAllowedFilesList("jpg,png,gif,txt,doc");
//设置禁止上传的文件类型
su.setDeniedFilesList("exe,bat,js,jsp,css");
//限制上传文件的大小 B
su.setMaxFileSize(1024*1024*3);
//设置编码方式
su.setCharset("utf-8");
//开始上传 内存
su.upload();
//①自动找到web项目的根目录
/*String path = "images\\";
File file = su.getFiles().getFile(0);
if(!file.isMissing()){//说明选择了文件
file.setCharset("utf-8");
path+=file.getFileName();
file.saveAs(path,SmartUpload.SAVE_VIRTUAL);
}*/
//②
String path = "images\\";
String webPath = request.getRealPath("/");
File file = su.getFiles().getFile(0);
if(!file.isMissing()){
file.setCharset("utf-8");
path+=file.getFileName();
file.saveAs(webPath+path);
}
Request req = su.getRequest();
String name = req.getParameter("iname");
out.print(webPath+path+" "+name);
%>
4、说明表单必须用enctype属性进行封装:
<form action="" method="post" enctype="multipart/form-data">
//表示表单将按照二进制的方式提交
在使用smartUpload时必须严格按照如上程序进行:
SmartUpload smart=new SmartUpload(); //实例化SmartUpload上传组件
smart.initialize(pageContext); //初始化上传操作
smart.upload(); //上传准备
smart.save("upload"); //将上传文件保存在upload文件夹中
a. 得到上传文件名:
String file=smart.getFiles().getFile(0).getFileName();
//获得第一个上传文件
b. 得到上传文件的后缀名:
String ext=smart.getFiles().getFile(0).getFileExt();
//获得第一个上传文件的文件后缀
c. 拼凑文件名:
String fileName=getIPTimeRand()+"."+ext;
d. 由于此时要用新的文件名称保存上传文件,所以要通过smart.getFiles().getFile(0).saveAs()方法进行手工保存。具体代码:
smart.getFiles().getFile(0).saveAs(getServletContext().getRealPath("/")+"upload"+java.io.File.separator+fileName);
//file.saveAs(路径,上传方式);
5、.设置字符编码:smart.setCharset(“utf-8”);
//设置字符编码(如果未设,当请求中含有中文时会报:java.lang.NullPointerException: charsetName),准备上传
6.为上传文件自动命名
如果多个用户上传的文件名称一样,则肯定会发生覆盖的情况,为了避免这类问题,可以采用为上传文件自动命名的方式。为防止重名,自动命名可以采用如下的格式:
IP地址 + 时间戳 + 三位随机数
public String getIPTimeRand(){
//实例化StringBuffer对象,用来做文件名拼接
StringBuffer buf=new StringBuffer();
//获得本机的IP地址
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString;//获得本机IP
address=addr.getHostName()toString;//获得本机名称
//获得当前时间戳
SimpleDateFormat date=new SimpleDateFormat("yyyyMMddHHmmssSSS");
date.format(new Date());
//获得一个3位数的随机数
Random r=new Random();
for(int i=0; i<3; i)
buf.append(r.nextInt(10));
}
return buf;
}
文件名得到了,我们需要得到上传文件的后缀名:getFileExt()
6、混合表单:
当一个表单使用了enctype封装后,其他的非文件类的表单控件的内容就无法通过request内置对象取得,此时必须通过SmartUpload类中提供的getRequest()方法取得全部的请求参数。
String uname=smart.getRequest().getParameter("uname");
注意:该句代码一定要在执行完upload()方法之后才可以使用
7、 批量上传:如要完成批量上传,则肯定需要循环的方式进行,那么就必须通过如下方法取得上传文件数量。
smart.getFiles().getCount();
form>表单代码:
<form action="smartupload.jsp" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="pic1"><br/>
文件2:<input type="file" name="pic2"><br/>
文件3:<input type="file" name="pic3"><br/>
<input type="submit" value="上传" >
<input type="reset" value="重置">
</form>
smartupload.jsp代码:
<%
SmartUpload smart=new SmartUpload();
smart.initialize(pageContext);
smart.upload();
for(int i=0; i<smart.getFiles().getCount(); i++){
smart.save("upload");
}
%>
8、关于在servlet中写smartupload的问题:
如果fckeditor在servlet中怎么写?使用了JspFactory.getDefaultFactory()方法
<要注意抛异常>
PageContext context = JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8*1024, true);
一共七个参数:
第一个参数是传递一个Servlet,在servlet中传递this就可以了;
第二个和第三个参数是request与response不多说明了;
第四个参数是发生错误后的url路径地址,如果没有可以键入null;
第五个参数是是否需要session,这里可以写入true;
第六个参数是缓存大小,我用了8*1024,也可以用JspWriter.DEFAULT_BUFFER;
第七个参数是是否需要刷新,键入ture;