fckeditor的用法,如何获得编辑器中的内容(转自http://suichunyu1987.blog.163.com/blog/static/87309653200981032351210/)

樊熠彤
2023-12-01
 

配置FCKeditor

 

§Fckdeitor/editor/fckconfig.js为fckdeitor的主要配置文件包含所有可配置的信息

§1.可以直接修改主配置文件fckdeitor.js

§2.定义单独的配置文件覆盖fckdeitor.js文件

     首先创建自己的一个JavaScript文件

      配置好自己的设置后在fckcofing.js中指定自定义的配置文件

     具体配置如下

     FCKConfig.Custom

ConfigurationPath=‘/工程名/配置文件名’

§3.在页面中创建fckeditor时指定配置文件(只对当前实例有效)oFCKeditor.config   [“CustomConfigurationsPath”]=“/工程名/配置文件名”

 

配置加载顺序

 

1.加载主配置文件fckconfig.js

2.加载自定义的配置文件(如果有),覆盖相同的配置项

3.使用对实例的配置覆盖相同的配置项(只对当前实例有效)

提示

   1.系统会自动检测并运用适当的界面语言

   2.不能删除主配置文件fckconfig.js

   3.修改配置后要清空浏览器缓存,以免影响结果

 

§自定义toolbarSet

1.直接修改工具集或应用自定义的工具集

2. 在配置文件中自定义工具集

   单引号引用的短语为一个功能‘—’表示一个分析符‘/’表示一个换行   对照显示效果进行修改

   使用方法在引用fckeditor时添加toolbarSet属性  例子:oFCKeditor.ToolbarSet=“自定义工具集名”

§加上常用字体

    1.在主配置文件中找到FCKConfig.FontNames选项并复制到自定义的配置文件中

    2.配置自己的字体:不同的字体之间用‘;’分隔  并且需要将配置文件的编码方式改为“utf-8”

 

§修改回车和shift+回车的换行行为

在主配置文件中找到

   FCKConfig.EnterMode=‘p’;

   FCKConfig.ShiftEnterMode=‘br’;

复制到自定义的配置文件中。改为:

   FCKConfig.EnterMode=‘br’;

   FCKConfig.ShiftEnterMode=‘p’;

§修改编辑区的样式文件

在主配置文件中找到

FCKConfig.EditorAreaCSS=FCKConfig.BasePath+’css/fck_editorarea.css’;

   找到css对其进行修改

 

§更换表情图片

 

§在主配置文件中找到相应的配置信息

§找到fck_smiley.html在onload方法中

§Dialog.SetAutoSize(true)自动调整窗口大小

    如果要显示滚动条

    在<body style=“overflow:hidden”>

    Visible:默认值。不剪切内容也不添加滚动条、

    Auto:在必需时对象内容才会被剪切或显示滚动条

    Hidden:不显示超过对象尺寸的内容

    Scroll:总是显示滚动条

 

自定义配置文件的路径设置

 

§FCKConfing.EditorPath+”myconfig.js”

§FCKConfing.EditorPath为fckeditor/editor的路径

myconfig.js为自定义的配置文件

 

 

文件上传

§在工程的web.xml中配置servlet

§第一步:

    <servlet>

          <servlet-name>Connector</servlet-name>

          <servlet-class> net.fckeditor.connector.ConnectorServlet </servlet-class>

          <load-on-startup>1</load-on-startup>

   </servlet>

    <servlet-mapping>

          <servlet-name>Connector</servlet-name>

          <url-pattern> /fckeditor/editor/filemanager/connectors/* </url-pattern>

    </servlet-mapping>

§第二步:

   在classpath下创建一个配置文件文件名为fckeditor.properties

    connector.userActionImpl=net.fckeditor.requestcycle.impl.UserActionImpl

无法使用中文信息

 修改此问题需要修改FCKeditor上传文件的servlet的源码

无法上传中文图片

   在servlet的post方法中设置

  upload.setHeaderEncoding("utf-8");

无法引用中文文件

 改变上传文件的名称

 filename=UUID.randomUUID ().toString()+"."+extension;

无法创建中文文件夹

 修改方法:

  在get中进行转码。

newFolderStr =new String(newFolderStr.getBytes("iso8859-1"),"utf-8");

 

 

一个修改好的servlet:

package test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.fckeditor.connector.ConnectorServlet;
import net.fckeditor.connector.Messages;
import net.fckeditor.handlers.CommandHandler;
import net.fckeditor.handlers.ConnectorHandler;
import net.fckeditor.handlers.ExtensionsHandler;
import net.fckeditor.handlers.RequestCycleHandler;
import net.fckeditor.handlers.ResourceTypeHandler;
import net.fckeditor.response.UploadResponse;
import net.fckeditor.response.XmlResponse;
import net.fckeditor.tool.Utils;
import net.fckeditor.tool.UtilsFile;
import net.fckeditor.tool.UtilsResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet to upload and browse files.<br />
 *
 * This servlet accepts 4 commands which interact with the server-side
 * filesystem.<br />
 * The allowed commands are:
 * <ul>
 * <li><code>GetFolders</code>: Retrieves a list of folders in the current
 * folder</li>
 * <li><code>GetFoldersAndFiles</code>: Retrives a list of files and folders
 * in the current folder</li>
 * <li><code>CreateFolder</code>: Creates a new folder in the current folder</li>
 * <li><code>FileUpload</code>: Stores an uploaded file into the current
 * folder. (must be sent with POST)</li>
 * </ul>
 *
 * @version $Id: ConnectorServlet.java 2101 2008-06-22 22:00:48Z mosipov $
 */
public class sui extends HttpServlet {

 private static final long serialVersionUID = -5742008970929377161L;
 private static final Logger logger = LoggerFactory.getLogger(sui.class);

 /**
  * Initialize the servlet: <code>mkdir</code> &lt;DefaultUserFilesPath&gt;
  */
 public void init() throws ServletException, IllegalArgumentException {
  String realDefaultUserFilesPath = getServletContext().getRealPath(
          ConnectorHandler.getDefaultUserFilesPath());

  File defaultUserFilesDir = new File(realDefaultUserFilesPath);
  UtilsFile.checkDirAndCreate(defaultUserFilesDir);

  logger.info("ConnectorServlet successfully initialized!");
 }

 /**
  * Manage the <code>GET</code> requests (<code>GetFolders</code>,
  * <code>GetFoldersAndFiles</code>, <code>CreateFolder</code>).<br/>
  *
  * The servlet accepts commands sent in the following format:<br/>
  * <code>connector?Command=&lt;CommandName&gt;&Type=&lt;ResourceType&gt;&CurrentFolder=&lt;FolderPath&gt;</code>
  * <p>
  * It executes the commands and then returns the result to the client in XML
  * format.
  * </p>
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
  logger.debug("Entering ConnectorServlet#doGet");

  response.setCharacterEncoding("UTF-8");
  response.setContentType("application/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");

  logger.debug("Parameter Command: {}", commandStr);
  logger.debug("Parameter Type: {}", typeStr);
  logger.debug("Parameter CurrentFolder: {}", currentFolderStr);

  XmlResponse xr;

  if (!RequestCycleHandler.isEnabledForFileBrowsing(request))
   xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.NOT_AUTHORIZED_FOR_BROWSING);
  else if (!CommandHandler.isValidForGet(commandStr))
   xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_COMMAND);
  else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr))
   xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_TYPE);
  else if (!UtilsFile.isValidPath(currentFolderStr))
   xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_CURRENT_FOLDER);
  else {
   CommandHandler command = CommandHandler.getCommand(commandStr);
   ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr);

   String typePath = UtilsFile.constructServerSidePath(request, resourceType);
   String typeDirPath = getServletContext().getRealPath(typePath);

   File typeDir = new File(typeDirPath);
   UtilsFile.checkDirAndCreate(typeDir);

   File currentDir = new File(typeDir, currentFolderStr);

   if (!currentDir.exists())
    xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME);
   else {

    xr = new XmlResponse(command, resourceType, currentFolderStr, UtilsResponse
            .constructResponseUrl(request, resourceType, currentFolderStr, true,
                    ConnectorHandler.isFullUrl()));

    if (command.equals(CommandHandler.GET_FOLDERS))
     xr.setFolders(currentDir);
    else if (command.equals(CommandHandler.GET_FOLDERS_AND_FILES))
     xr.setFoldersAndFiles(currentDir);
    else if (command.equals(CommandHandler.CREATE_FOLDER)) {
     String newFolderStr = UtilsFile.sanitizeFolderName(request
             .getParameter("NewFolderName"));
     newFolderStr =new String(newFolderStr.getBytes("iso8859-1"),"utf-8");//用来解决无法创建中文文件夹问题
     logger.debug("Parameter NewFolderName: {}", newFolderStr);

     File newFolder = new File(currentDir, newFolderStr);
     int errorNumber = XmlResponse.EN_UKNOWN;

     if (newFolder.exists())
      errorNumber = XmlResponse.EN_ALREADY_EXISTS;
     else {
      try {
       errorNumber = (newFolder.mkdir()) ? XmlResponse.EN_OK
               : XmlResponse.EN_INVALID_FOLDER_NAME;
      } catch (SecurityException e) {
       errorNumber = XmlResponse.EN_SECURITY_ERROR;
      }
     }
     xr.setError(errorNumber);
    }
   }
  }

  out.print(xr);
  out.flush();
  out.close();
  logger.debug("Exiting ConnectorServlet#doGet");
 }

 /**
  * Manage the <code>POST</code> requests (<code>FileUpload</code>).<br />
  *
  * The servlet accepts commands sent in the following format:<br />
  * <code>connector?Command=&lt;FileUpload&gt;&Type=&lt;ResourceType&gt;&CurrentFolder=&lt;FolderPath&gt;</code>
  * with the file in the <code>POST</code> body.<br />
  * <br>
  * It stores an uploaded file (renames a file if another exists with the
  * same name) and then returns the JavaScript callback.
  */
 @SuppressWarnings("unchecked")
 public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
  logger.debug("Entering Connector#doPost");

  response.setCharacterEncoding("UTF-8");
  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");

  logger.debug("Parameter Command: {}", commandStr);
  logger.debug("Parameter Type: {}", typeStr);
  logger.debug("Parameter CurrentFolder: {}", currentFolderStr);

  UploadResponse ur;

  // if this is a QuickUpload request, 'commandStr' and 'currentFolderStr'
  // are empty
  if (Utils.isEmpty(commandStr) && Utils.isEmpty(currentFolderStr)) {
   commandStr = "QuickUpload";
   currentFolderStr = "/";
  }

  if (!RequestCycleHandler.isEnabledForFileUpload(request))
   ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR, null, null,
           Messages.NOT_AUTHORIZED_FOR_UPLOAD);
  else if (!CommandHandler.isValidForPost(commandStr))
   ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_COMMAND);
  else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr))
   ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_TYPE);
  else if (!UtilsFile.isValidPath(currentFolderStr))
   ur = UploadResponse.UR_INVALID_CURRENT_FOLDER;
  else {
   ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr);

   String typePath = UtilsFile.constructServerSidePath(request, resourceType);
   String typeDirPath = getServletContext().getRealPath(typePath);

   File typeDir = new File(typeDirPath);
   UtilsFile.checkDirAndCreate(typeDir);
   File currentDir = new File(typeDir, currentFolderStr);
   if (!currentDir.exists())
    ur = UploadResponse.UR_INVALID_CURRENT_FOLDER;
   else {

    String newFilename = null;
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
                //改变编码方式
    upload.setHeaderEncoding("utf-8");
    try {

     List<FileItem> items = upload.parseRequest(request);
     // We upload only one file at the same time
     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);
     //改变引用中文图片乱码问题
                    filename=UUID.randomUUID().toString()+"."+extension;//extension为后缀名

     if (!ExtensionsHandler.isAllowed(resourceType, extension))
      ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION);
     else {

      // construct an unique file name
      
      File pathToSave = new File(currentDir, filename);//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);

      // secure image check
      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) {
     ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR);
    }
   }

  }

  out.print(ur);
  out.flush();
  out.close();

  logger.debug("Exiting Connector#doPost");
 }

}
不要忘记修改自己的web.xml

<servlet>
      <servlet-name>Connector</servlet-name>
        <servlet-class>
          test.sui <!--改为自己定义的servlet名  -->
      </servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>Connector</servlet-name>
      <url-pattern>
        /fckeditor/editor/filemanager/connectors/*
      </url-pattern>
    </servlet-mapping>

 类似资料: