09-02
17
JSP中fckeditor控件-文件类型与大小的控制
作者:Java伴侣 日期:2009-02-17
(一)文件类型的控制
此处只是演示图片类型的控制,其它的控制需要查看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,修改方式如下:
(二)文件大小的控制
修改ConnectorServlet.java中文件上传前的判断
在WebRoot/fckeditor/editor/dialog/fck_image/fck_image.js中对自定义错误进行处理。
如果限制的事其它类型的文件大小,需要去WebRoot/fckeditor/editor/dialog/目录下修改对应的js文件。
此处只是演示图片类型的控制,其它的控制需要查看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)$" ;
在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)
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 ;
}
{
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 ;
}
评论: 0 | 引用: 0 | 查看次数: 553
发表评论