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中文件上传前的判断

  1. try {
  2.                     List<FileItem> items = upload.parseRequest(request);
  3.                     FileItem uplFile = items.get(0);
  4.                     String rawName = UtilsFile.sanitizeFileName(uplFile.getName());
  5.                     String filename = FilenameUtils.getName(rawName);
  6.                     String baseName = FilenameUtils.removeExtension(filename);
  7.                     String extension = FilenameUtils.getExtension(filename);
  8.                     //如果这个文件的扩展名不允许上传
  9.                     if (!ExtensionsHandler.isAllowed(resourceType, extension))
  10.                         ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION);
  11.                     //如果文件大小超过限制
  12.                     else if(uplFile.getSize()>10*1024){
  13.                     //传递一个自定义的错误码204
  14.                         ur = new UploadResponse(204);
  15.                     }
  16.                     
  17.                     //如果不存在以上情况,则保存
  18.                     else {
  19.                         filename=UUID.randomUUID().toString()+"."+extension;
  20.                         File pathToSave = new File(currentDir, filename);
  21.                         int counter = 1;
  22.                         while (pathToSave.exists()) {
  23.                             newFilename = baseName.concat("(").concat(String.valueOf(counter))
  24.                                     .concat(")").concat(".").concat(extension);
  25.                             pathToSave = new File(currentDir, newFilename);
  26.                             counter++;
  27.                         }
  28.                         if (Utils.isEmpty(newFilename))
  29.                             ur = new UploadResponse(UploadResponse.SC_OK, UtilsResponse
  30.                                     .constructResponseUrl(request, resourceType, currentFolderStr,
  31.                                             true, ConnectorHandler.isFullUrl()).concat(filename));
  32.                         else
  33.                             ur = new UploadResponse(UploadResponse.SC_RENAMED,
  34.                                     UtilsResponse.constructResponseUrl(request, resourceType,
  35.                                             currentFolderStr, true, ConnectorHandler.isFullUrl())
  36.                                             .concat(newFilename), newFilename);
  37.                         if (resourceType.equals(ResourceTypeHandler.IMAGE)
  38.                                 && ConnectorHandler.isSecureImageUploads()) {
  39.                             if (UtilsFile.isImage(uplFile.getInputStream()))
  40.                                 uplFile.write(pathToSave);
  41.                             else {
  42.                                 uplFile.delete();
  43.                                 ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION);
  44.                             }
  45.                         } else
  46.                             uplFile.write(pathToSave);
  47.                     }
  48.                 } catch (Exception e)
在WebRoot/fckeditor/editor/dialog/fck_image/fck_image.js中对自定义错误进行处理。
  1.     switch ( errorNumber )
  2.     {
  3.         case 0 :    // No errors
  4.             alert( '文件上传成功' ) ;
  5.             break ;
  6.         case 1 :    // Custom error
  7.             alert( customMsg ) ;
  8.             return ;
  9.         case 101 :  // Custom warning
  10.             alert( customMsg ) ;
  11.             break ;
  12.         case 201 :
  13.             alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
  14.             break ;
  15.         case 202 :
  16.             alert( 'Invalid file type' ) ;
  17.             return ;
  18.         case 203 :
  19.             alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
  20.             return ;
  21.         case 204 :
  22.             alert( "图片文件大小超出10K" ) ;
  23.             return ;
  24.         case 500 :
  25.             alert( 'The connector is disabled' ) ;
  26.             break ;
  27.         default :
  28.             alert( 'Error on file upload. Error number: ' + errorNumber ) ;
  29.             return ;
  30.     }
如果限制的事其它类型的文件大小,需要去WebRoot/fckeditor/editor/dialog/目录下修改对应的js文件。

你可能感兴趣的:(jsp,fckeditor,扩展,extension)