1.
SmartUpload组件特点 使用简单:编写少量代码,
完成上传下载功能
能够控制上传内容 能够控制上传文件的大小、类型
缺点:目前已停止更新服务
2. CKEditor编辑器
1.ckeditor jAr包
SmartUpload smart=new SmartUpload(); //实例化SmartUpload上传组件
smart.initialize(pageContext); //初始化上传操作
smart.upload(); //上传准备
smart.save("upload"); //将上传文件保存在upload文件夹中
2.限制文件的上传类型
允许上传类型:
samrt.setAllowedFilesList("gif,jpg,doc,rar");
不允许上传类型:
samrt.setDeniedFilesList("jsp,asp,php,aspx");
3.限制文件上传的大小
smart.setMaxFileSize(2*1024*1024);
4.设置字符编码
smart.setCharset("utf-8");
//设置字符编码(如果未设,当请求中含有中文时会报:java.lang.NullPointerException: charsetName),准备上传
5.混合表单
当一个表单使用了enctype封装后,其他的非文件类的表单控件的内容就无法通过request内置对象取得,此时必须通过SmartUpload类中提供的getRequest()方法取得全部的请求参数。
String uname=smart.getRequest().getParameter("uname");
注意:该句代码一定要在执行完upload()方法之后才可以使用
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()
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(路径,上传方式);
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");
}
%>
================================================================
关于在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;
<%@ page language="java" import="java.util.*,java.sql.*,com.jspsmart.upload.*" pageEncoding="gbk"%>
<%@page import="java.text.SimpleDateFormat"%>
<%
SmartUpload su = new SmartUpload();
//初始化SmartUpload对象
su.initialize(pageContext);
com.jspsmart.upload.File file = null;
//允许上传类型
String allowed = "gif,jpg,doc,rar";
//不许上传类型
String denied = "jsp,asp,php,aspx,html,htm,exe,bat";
com.jspsmart.upload.Request req = null;
//设置上传文件大小
int file_size = 2*1024*1024;
String exceptionMsg = null;
int i = 0;
try {
//定义允许上传文件类型
su.setAllowedFilesList(allowed);
//不允许上传文件类型
su.setDeniedFilesList(denied);
//单个文件最大限制
su.setMaxFileSize(file_size);
su.setCharset("GBK");
//执行上传
su.upload();
//得到单个上传文件的信息
file = su.getFiles().getFile(0);
String filepath = null;
if(!file.isMissing()){
//设置文件在服务器的保存位置
filepath = "upload\\";
filepath += file.getFileName();
//文件另存为
file.setCharset("gbk");
System.out.println(filepath);
file.saveAs(filepath, SmartUpload.SAVE_VIRTUAL);
}
} catch (Exception e) {
exceptionMsg = e.getMessage();
e.printStackTrace();
}
%>