一、ckeditor (富文本编辑器)
<p>
<label> 内容 </label>
<textarea name="ncontent" cols="70" rows="10"></textarea>
<script>
CKEDITOR.replace( 'ncontent' );
</script>
</p>
二、smartupload (文件上传功能)
SmartUpload组件特点
使用简单:编写少量代码,完成上传下载功能
能够控制上传内容 能够控制上传文件的大小、类型
缺点:目前已停止更新服务
需要设置表单的enctype属性 :<form enctype="multipart/form-data" method="post">
<form action="doindex.jsp" enctype="multipart/form-data" method="post"> 用户名:<input type="text" name="iname"></br> 头像:<input type="file" name="ifile" multiple="multiple"></br> <input type="submit" value="注册"> <input type="reset" value="清空"> </form>
设置相关需求
/实例化
SmartUpload su=new SmartUpload();
//初始化 pageContext是jsp的九大内置对象之一 作用域仅限于当前的jsp页面
su.initialize(pageContext);
//设置相关要求
su.setCharset("utf-8");//设置编码方式
su.setAllowedFilesList("jpg,jsp,png,jpeg,gif");//设置允许上传的文件类型 用逗号隔开
su.setDeniedFilesList("exe,jsp,js,bat");//设置禁止上传的文件类型
su.setMaxFileSize(1024*1024*2);//B 设置文件的最大大小 不超过2M
//上传到服务器内存
su.upload();
单文件上传
自动版找web项目的根目录
String path="images/"; //获取文件 File file=su.getFiles().getFile(0); //做判断 if(!file.isMissing()){//上传了文件 //设置文件的编码方式 file.setCharset("utf-8"); path+=file.getFileName();//拼接上原有的文件名 images/2.gif file.saveAs(path, SmartUpload.SAVE_VIRTUAL);//自动找web项目的根目录 } out.print(path); */
手动版找web项目的根目录 文件上传
String path="images/"; //底层路径 //E:\S2_t279\javaweb\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\q6\ String webPath=this.getServletContext().getRealPath("/"); //获取文件 File file=su.getFiles().getFile(0); //做判断 if(!file.isMissing()){//上传了文件 //设置文件的编码方式 file.setCharset("utf-8"); path+=file.getFileName();//拼接上原有的文件名 images/2.gif file.saveAs(webPath+path);//手动找web项目的根目录 } out.print(path);
多文件上传
//多文件上传
//拿到所有的文件
Files fs=su.getFiles();
//拿到总数目 循环
for(int i=0;i<fs.getCount();i++){
//依次拿到每一个文件 依次进行上传
File file= fs.getFile(i);
String path="images/";
//做判断
if(!file.isMissing()){//上传了文件
//设置文件的编码方式
file.setCharset("utf-8");
path+=file.getFileName();//拼接上原有的文件名 images/2.gif
file.saveAs(path, SmartUpload.SAVE_VIRTUAL);//自动找web项目的根目录
}
out.print(path+"<br/>");
}