fckeditor默认不支持文件上传,需要下载web服务器端程序(fckeditor.java),并进行配置
1.在web.xml中加入ConnectorServlet的配置信息
- <servlet>
- <servlet-name>Connector</servlet-name>
- <servlet-class>
- net.fckeditor.connector.ConnectorServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Connector</servlet-name>
- <url-pattern>
- /fckeditor/editor/filemanager/connectors/*
- </url-pattern>
- </servlet-mapping>
<servlet> <servlet-name>Connector</servlet-name> <servlet-class> net.fckeditor.connector.ConnectorServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Connector</servlet-name> <url-pattern> /fckeditor/editor/filemanager/connectors/* </url-pattern> </servlet-mapping>
2.在classpath路径上创建fckeditor.properties(在src文件夹下创建)
- connector.userActionImpl=net.fckeditor.requestcycle.impl.UserActionImpl
connector.userActionImpl=net.fckeditor.requestcycle.impl.UserActionImpl
乱码问题
由于fckeditor未考虑中文乱码问题,所以需要对ConnectorServlet做修改
解决fckeditor创建文件夹中文乱码问题
在ConnectorServlet的doGet方法中找到String newFolderStr = UtilsFile.sanitizeFolderName(newFolderName);这行代码,在其上添加如下2行代码。
- //NewFolderName为新创建的文件夹名称,先用iso-8859-1编码将字符串还原成字节数组,在用utf-8重新编码
- String newFolderName = request.getParameter("NewFolderName");
- newFolderName = new String(newFolderName.getBytes("iso-8859-1"), "utf-8");
- String newFolderStr = UtilsFile.sanitizeFolderName(newFolderName);
//NewFolderName为新创建的文件夹名称,先用iso-8859-1编码将字符串还原成字节数组,在用utf-8重新编码 String newFolderName = request.getParameter("NewFolderName"); newFolderName = new String(newFolderName.getBytes("iso-8859-1"), "utf-8"); String newFolderStr = UtilsFile.sanitizeFolderName(newFolderName);
解决上传文件名为中文文件时出现乱码
fckeditor在java平台采用的是commons-upload组件进行文件上传,只要修改ServletFileUpload的headerEncoding属性为utf-8就能解决上传文件名是中文时所出现的乱码问题。在ConnectorServlet的doPost方法中找到ServletFileUpload upload = new ServletFileUpload(factory);在其后加入下面代码
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- //解决上传文件名为中文名时出现乱码
- upload.setHeaderEncoding("utf-8");
FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); //解决上传文件名为中文名时出现乱码 upload.setHeaderEncoding("utf-8");
中文图片不能引用
修改server.xml 端口为8080的Connector,添加属性URIEncoding="UTF-8"
- <Connector port="8080" maxHttpHeaderSize="8192"
- maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
- enableLookups="false" redirectPort="8443" acceptCount="100"
- connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />
解决上传文件名重名问题
上传文件名用uuid随机生成的32位字符串代替,防止文件名重复
- String filename = FilenameUtils.getName(rawName);
- String baseName = FilenameUtils.removeExtension(filename);
- String extension = FilenameUtils.getExtension(filename);
- //上传文件名用uuid随机生成的32位字符串代替,防止文件名重复
- filename=UUID.randomUUID().toString().replace("-", "")+"."+extension;
String filename = FilenameUtils.getName(rawName); String baseName = FilenameUtils.removeExtension(filename); String extension = FilenameUtils.getExtension(filename); //上传文件名用uuid随机生成的32位字符串代替,防止文件名重复 filename=UUID.randomUUID().toString().replace("-", "")+"."+extension;
控制允许上传文件的文件类型
fckeditor把上传的文件分为四种:file,Image,Flash,Media
fckeditor会对上传文件的类型进行两次验证(前台javascript验证和后台java验证)所以需要对myconfig.js和fckeditor.propertise进行配置
例如:限制上传图片格式为gif和png
在myconfig.js增加下面配置(可参看fckconfig.js的具体配置选项)
- FCKConfig.ImageUploadAllowedExtensions = ".(gif|png)$" ;
FCKConfig.ImageUploadAllowedExtensions = ".(gif|png)$" ;
在fckeditor.propertise进行配置(可参看)
- connector.resourceType.image.extensions.allowed=gif|png
connector.resourceType.image.extensions.allowed=gif|png
控制允许上传文件的文件大小
1.在服务端的servlet中,在保存文件之前先判断一下文件大小,如果超出限制,就传递一个自定义的错误码,并且不再保存文件,在ConnectorServlet的doPost方法中找到if (!ExtensionsHandler.isAllowed(resourceType, extension))
在后面添加else if语句块
- //如果文件的扩展名不允许上传
- if (!ExtensionsHandler.isAllowed(resourceType, extension))
- ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION);
- //如果文件大小超出限制10k
- else if(uplFile.getSize()>10*1024){
- //传递一个自定义的错误码
- ur = new UploadResponse(204);
- }
- //如果不存在以上错误,则保存文件
- else {
- ...
- }
//如果文件的扩展名不允许上传 if (!ExtensionsHandler.isAllowed(resourceType, extension)) ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); //如果文件大小超出限制10k else if(uplFile.getSize()>10*1024){ //传递一个自定义的错误码 ur = new UploadResponse(204); } //如果不存在以上错误,则保存文件 else { ... }
2.修改对应的页面中的回调函数,增加对这个自定义的错误码的处理
找到fckeditor/editor/filemanager/browser/default/frmupload.html和fckeditor/editor/dialog/fck_image/fck_image.js中的OnUploadCompleted方法,
在switch 语句块中添加如下代码
- case 204 :
- alert( '文件太大' ) ;
- break ;