在利用fckeditor上傳文件的過程中,需要
1、限制文件上傳的大小。
2、限制不同的用戶上傳的文件到不同的目錄。
3、限制上傳后的圖片的高度和寬度
4、限制上傳圖片或者文件的后綴名
自己在做工程的過程中,對于以上問題進(jìn)行了解決,在此記錄以作備份。
1、在SimpleUploaderServlet.java中
if (debug) System.out.println("--- BEGIN DOPOST ---");
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out = response.getWriter();
String typeStr=request.getParameter("Type");
String currentPath=baseDir+typeStr;
String currentDirPath=getServletContext().getRealPath(currentPath);
currentPath=request.getContextPath()+currentPath;
if (debug) System.out.println(currentDirPath);
String retVal="0";
String newName="";
String fileUrl="";
String errorMessage="";
if(enabled) {
DiskFileUpload upload = new DiskFileUpload();
upload.setHeaderEncoding("utf-8");
try {
List items = upload.parseRequest(request);
Map fields=new HashMap();
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
fields.put(item.getFieldName(),item.getString());
else
fields.put(item.getFieldName(),item);
}
FileItem uplFile=(FileItem)fields.get("NewFile");
String fileNameLong=uplFile.getName();
fileNameLong=fileNameLong.replace('\\','/');
String[] pathParts=fileNameLong.split("/");
String fileName=pathParts[pathParts.length-1];
//String nameWithoutExt=getNameWithoutExtension(fileName);
String ext=getExtension(fileName);
Date dt = new Date(System.currentTimeMillis());
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
fileName = fmt.format(dt)+"."+ext;//取得新文件名
String nameWithoutExt=getNameWithoutExtension(fileName); //這段代碼位置不能在獲取新文件名前面
File pathToSave=new File(currentDirPath,fileName);
fileUrl=currentPath+"/"+fileName;
if(extIsAllowed(typeStr,ext)) {
int counter=1;
while(pathToSave.exists()){
newName=nameWithoutExt+"("+counter+")"+"."+ext;
fileUrl=currentPath+"/"+newName;
retVal="201";
pathToSave=new File(currentDirPath,newName);
counter++;
}
if(uplFile.getSize()>=1024*1024){
retVal="204";
if (debug) System.out.println("204 error");
}
uplFile.write(pathToSave);
}
else {
retVal="202";
errorMessage="";
if (debug) System.out.println("Invalid file type: " + ext);
}
}catch (Exception ex) {
if (debug) ex.printStackTrace();
retVal="203";
}
}
else {
retVal="1";
errorMessage="This file uploader is disabled. Please check the WEB-INF/web.xml file";
}
out.println("<script type=\"text/javascript\">");
out.println("window.parent.OnUploadCompleted("+retVal+",'"+fileUrl+"','"+newName+"','"+errorMessage+"');");
out.println("</script>");
out.flush();
out.close();
if (debug) System.out.println("--- END DOPOST ---");
然后在
\fckeditor\editor\dialog\fck_image\fck_image.js
\fckeditor\editor\dialog\fck_image\fck_flash.js
\fckeditor\editor\dialog\fck_image\fck_link.js
中修改相應(yīng)的錯誤提示。(搜索switch)
2、在ConnectorServlet.java中
修改doGet方法
///////////////////////////////////////////////////////////
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out = response.getWriter();
String commandStr=request.getParameter("Command");
String typeStr=request.getParameter("Type");
String currentFolderStr=request.getParameter("CurrentFolder");
String currentPath=baseDir+typeStr+currentFolderStr;
String currentDirPath=getServletContext().getRealPath(currentPath);
//修改文件上傳的目錄
String basedir = "E:\\www_xndsj\\upload\\image";
File baseFile = new File(basedir);
if(!baseFile.exists())
{
System.out.println("Fckeditor文件存放路徑錯誤,請修改!");
if(!baseFile.mkdir())
{
System.out.println("Fckeditor文件創(chuàng)建錯誤!");
}
}
//獲取公司ID
String corp_id = (String)request.getSession().getAttribute("corp_id");
//如果不是公司修改自己的信息,默認(rèn)圖片路徑是0
if(corp_id == null||"".equals(corp_id))
corp_id = "0" ;
//當(dāng)前本地路徑
currentDirPath = basedir + "/user_" + corp_id;
//修改currentPath
currentPath = "/upload/image/user_" + corp_id +"/";
//////////////////////////////////////////////
根據(jù)獲取的不同的corp_id建立不同的目錄
修改doPost方法
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out = response.getWriter();
String commandStr=request.getParameter("Command");
String typeStr=request.getParameter("Type");
String currentFolderStr=request.getParameter("CurrentFolder");
String currentPath=baseDir+typeStr+currentFolderStr;
String currentDirPath=getServletContext().getRealPath(currentPath);
////////////////////////////////////////
//修改文件上傳的目錄
String basedir = "E:\\www_xndsj\\upload\\image";
File baseFile = new File(basedir);
if(!baseFile.exists())
{
System.out.println("Fckeditor文件存放路徑錯誤,請修改!");
if(!baseFile.mkdir())
{
System.out.println("Fckeditor文件創(chuàng)建錯誤!");
}
}
//獲取公司ID
String corp_id = (String)request.getSession().getAttribute("corp_id");
//如果不是公司修改自己的信息,默認(rèn)圖片路徑是0
if(corp_id == null||"".equals(corp_id))
corp_id = "0" ;
//當(dāng)前本地路徑
currentDirPath = basedir + "/user_" +corp_id;
//////////////////////////////////////////////
3、在文件fckeditor/dialog/fck_image/fck_image.js修改以下內(nèi)容;
插入到function ok(){
}中
//限制寬度開始
if(!(GetE('txtWidth').value>0||GetE('txtWidth').value<750))
{
window.parent.SetSelectedTab( 'Info' ) ;
alert('圖片的寬度范圍為0--750,請重新輸入') ;
GetE('txtWidth').value='';
GetE('txtWidth').focus() ;
return false;
}
if(GetE('txtWidth').value.length==0)
{
window.parent.SetSelectedTab( 'Info' ) ;
GetE('txtWidth').focus();
alert( '請輸入圖像寬度,圖像寬度不能超過750' ) ;
return false ;
}
if(GetE('txtWidth').value>750)
{
window.parent.SetSelectedTab( 'Info' ) ;
alert('圖片的寬度不能超過750,請重新輸入') ;
GetE('txtWidth').value='';
GetE('txtWidth').focus() ;
return false;
}
if(GetE('txtWidth').value<0)
{
window.parent.SetSelectedTab( 'Info' ) ;
alert('圖片的寬度不能為負(fù),請重新輸入') ;
GetE('txtWidth').value='';
GetE('txtWidth').focus() ;
return false;
}
if(GetE('txtHeight').value<0)
{
window.parent.SetSelectedTab( 'Info' ) ;
alert('圖片的高度不能為負(fù),請重新輸入') ;
GetE('txtHeight').value='';
GetE('txtHeight').focus() ;
return false;
}
//限制寬度結(jié)束
4、同1.





