打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Fckeditor,JSP中 限制上传的大小 不同的用户上传到不同目录 限制上传图片的高度宽度

在利用fckeditor上传文件的过程中,需要

1、限制文件上传的大小。

2、限制不同的用户上传的文件到不同的目录。

3、限制上传后的图片的高度和宽度

4、限制上传图片或者文件的后缀名

自己在做工程的过程中,对于以上问题进行了解决,在此记录以作备份。

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
中修改相应的错误提示。(搜索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文件创建错误!");
  }   
 }
 
 //获取公司ID
 String corp_id = (String)request.getSession().getAttribute("corp_id");
 //如果不是公司修改自己的信息,默认图片路径是0
 if(corp_id == null||"".equals(corp_id))
    corp_id = "0" ;
 //当前本地路径
 currentDirPath = basedir + "/user_" + corp_id;
 //修改currentPath
 currentPath = "/upload/image/user_" + corp_id +"/";
 //////////////////////////////////////////////

  根据获取的不同的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文件创建错误!");
  }   
 }
 
 //获取公司ID
 String corp_id = (String)request.getSession().getAttribute("corp_id");
 //如果不是公司修改自己的信息,默认图片路径是0
 if(corp_id == null||"".equals(corp_id))
    corp_id = "0" ;
 //当前本地路径
 currentDirPath = basedir + "/user_" +corp_id;
 //////////////////////////////////////////////

 

3、在文件fckeditor/dialog/fck_image/fck_image.js修改以下内容;

插入到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('图片的宽度不能为负,请重新输入')  ;
         GetE('txtWidth').value='';
          GetE('txtWidth').focus() ;
          return false;
     } 
     if(GetE('txtHeight').value<0)
     {
          window.parent.SetSelectedTab( 'Info' ) ;
          alert('图片的高度不能为负,请重新输入')  ;
         GetE('txtHeight').value='';
          GetE('txtHeight').focus() ;
          return false;
     }
   //限制宽度结束

4、同1.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JAVA中Map集合的使用举例
四大 FCKeditor 实战应用技巧(1)
selenium web driver 实现截图功能
Ext中使用fckeditor
通过IDE开发基于web的XFire(WebService
fckeditor文本过滤方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服