application&富文本编辑器&文件上传
1.application
1.1 application对象:
Application 的作用范围在服务器一开始执行服务,到服务器关闭为止Application 的范围最、停留的时间也最久,所以使用时要特别注意不然可能会造成服务器负载越来越重的情况。只要将数据存入application对象,数据的范围范围 (Scope) 就为Application。
具有application范围的对象被绑定到javax.servlet.ServletContext中。在Web应用程序运行期间,所有的页面都可以访问在这个范围内的对象。
1.2 application对象的主要方法:
-
getAttribute(String name) return Object
-
getAttributeNames() return Enumeration
-
getInitParameter(String name)
-
getServletInfo()
-
setAttribute(String name , Object object)
2.富文本编辑器(CKeditor4)
2.1第一步:导入ckeditor插件到项目中
2.2第二步:在页面引入CKeditor.js文件
2.3第三步:写一个文本域 必须给标签添加一个name属性,添加class选择器起名为:ckedid
3.文件上传
- 创建smartupload对象
SmartUpload su = new SmartUpload(); - 设置smartupload编码
su.setCharset(“utf-8”); - 初始化 smartupload
su.initialize(this.getServletConfig(),req,resp); - 设置允许上传的文件类型
su.setAllowedFilesList(“gif,png”); - 设置不允许上传的文件类型
su.setDeniedFilesList(“exe,jpg,mp3,avi,mp4”);
设置允许上传的文件大小
su.setMaxFileSize(1024*1024);//单位:byte - 准备上传
su.upload(); - 获取选择的第一个文件 多文件上传 单文件上传
File file = su.getFiles().getFile(0); - 判断是否选择了文件
if(!file.isMissing()) {// 选择了文件 sa.jpg
/**
* 文件路径中间的间隔符:
* windows:
* linux:/
* windows:c:\test\sa.jsp
* linux:c:/test/sa.jsp
*/ - 拼接文件路径
path = “upload”+java.io.File.separator+file.getFileName(); - 上传
file.saveAs(path);
把上传的图片路径path保存到session作用域里面
req.getSession().setAttribute(“path”, path);