09-02
17

JSP中fckeditor控件-文件类型与大小的控制

(一)文件类型的控制
此处只是演示图片类型的控制,其它的控制需要查看WebRoot/fckeditor/editor/dialog/下面对应的 js文件
fckeditor.properties写法参考fckeditor-java-core-2.4.1.jar/net.fckeditor.handlers/net.fckeditor.handlers/default.properties文件
myconfig.js写法参考WebRoot/fckeditor/fckconfig.js文件

如果想添加一种新的图片类型如.abc,修改方式如下:
在fckeditor.properties添加属性connector.resourceType.image.extensions.allowed = bmp|gif|jpeg|jpg|png|abc
在myconfig.js中添加FCKConfig.ImageUploadAllowedExtensions = ".(jpg|gif|jpeg|png|bmp|abc)$" ;


(二)文件大小的控制

修改ConnectorServlet.java中文件上传前的判断


try {
                    List<FileItem> items = upload.parseRequest(request);
                    FileItem uplFile = items.get(0);
                    String rawName = UtilsFile.sanitizeFileName(uplFile.getName());
                    String filename = FilenameUtils.getName(rawName);
                    String baseName = FilenameUtils.removeExtension(filename);
                    String extension = FilenameUtils.getExtension(filename);
                    //如果这个文件的扩展名不允许上传
                    if (!ExtensionsHandler.isAllowed(resourceType, extension))
                        ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION);
                    //如果文件大小超过限制
                    else if(uplFile.getSize()>10*1024){
                    //传递一个自定义的错误码204
                        ur = new UploadResponse(204);
                    }
                    
                    //如果不存在以上情况,则保存
                    else {
                        filename=UUID.randomUUID().toString()+"."+extension;
                        File pathToSave = new File(currentDir, filename);
                        int counter = 1;
                        while (pathToSave.exists()) {
                            newFilename = baseName.concat("(").concat(String.valueOf(counter))
                                    .concat(")").concat(".").concat(extension);
                            pathToSave = new File(currentDir, newFilename);
                            counter++;
                        }
                        if (Utils.isEmpty(newFilename))
                            ur = new UploadResponse(UploadResponse.SC_OK, UtilsResponse
                                    .constructResponseUrl(request, resourceType, currentFolderStr,
                                            true, ConnectorHandler.isFullUrl()).concat(filename));
                        else
                            ur = new UploadResponse(UploadResponse.SC_RENAMED,
                                    UtilsResponse.constructResponseUrl(request, resourceType,
                                            currentFolderStr, true, ConnectorHandler.isFullUrl())
                                            .concat(newFilename), newFilename);
                        if (resourceType.equals(ResourceTypeHandler.IMAGE)
                                && ConnectorHandler.isSecureImageUploads()) {
                            if (UtilsFile.isImage(uplFile.getInputStream()))
                                uplFile.write(pathToSave);
                            else {
                                uplFile.delete();
                                ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION);
                            }
                        } else
                            uplFile.write(pathToSave);

                    }
                } catch (Exception e)


在WebRoot/fckeditor/editor/dialog/fck_image/fck_image.js中对自定义错误进行处理。

    switch ( errorNumber )
    {
        case 0 :    // No errors
            alert( '文件上传成功' ) ;
            break ;
        case 1 :    // Custom error
            alert( customMsg ) ;
            return ;
        case 101 :  // Custom warning
            alert( customMsg ) ;
            break ;
        case 201 :
            alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
            break ;
        case 202 :
            alert( 'Invalid file type' ) ;
            return ;
        case 203 :
            alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
            return ;
        case 204 :
            alert( "图片文件大小超出10K" ) ;
            return ;
        case 500 :
            alert( 'The connector is disabled' ) ;
            break ;
        default :
            alert( 'Error on file upload. Error number: ' + errorNumber ) ;
            return ;
    }
如果限制的事其它类型的文件大小,需要去WebRoot/fckeditor/editor/dialog/目录下修改对应的js文件。


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: FCK
相关日志:
评论: 0 | 引用: 0 | 查看次数: 553
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭