欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jsp中使用FCK2.6

程序员文章站 2022-03-20 12:17:20
...

1. 下载fckeditor-java-demo-2.6.war和fckeditor-java-2.6-bin.tar.gz

    下载地址: http://sourceforge.net/projects/fckeditor/files/FCKeditor.Java/2.6/

 

2. 解压fckeditor-java-demo-2.6.war, 获取以下资源

     主目录下的: fckeditor文件夹

 

     lib目录下的:

       commons-fileupload-1.2.1.jar

       commons-io-1.3.2.jar

       imageinfo-1.9.jar

       java-core-2.6.jar

       slf4j-api-1.5.8.jar

       slf4j-simple-1.5.8.jar

 

     WEB-INF\classes目录下的:     

       fckeditor.properties

 

     web.xml中的:

  <servlet>
    <servlet-name>ConnectorServlet</servlet-name>
    <servlet-class>
      net.fckeditor.connector.ConnectorServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>ConnectorServlet</servlet-name>
    <!-- Do not wrap this line otherwise Glassfish will fail to load this file -->
    <url-pattern>/fckeditor/editor/filemanager/connectors/*</url-pattern>
  </servlet-mapping>

 

 

3. 建立web项目sample

  将fckeditor文件夹sample\WebContent\plugins\fckeditor

 

  在lib中放入

       commons-fileupload-1.2.1.jar

       commons-io-1.3.2.jar

       imageinfo-1.9.jar

       java-core-2.6.jar

       slf4j-api-1.5.8.jar

       slf4j-simple-1.5.8.jar

 

  在sample\src中放入(eclipse自动编译后会到classes目录下)

       fckeditor.properties

 

  在web.xml中加入

  <servlet>
    <servlet-name>ConnectorServlet</servlet-name>
    <servlet-class>
      net.fckeditor.connector.ConnectorServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>ConnectorServlet</servlet-name>
    <!-- Do not wrap this line otherwise Glassfish will fail to load this file -->
    <url-pattern>/plugins/fckeditor/editor/filemanager/connectors/*</url-pattern>
  </servlet-mapping>

 

 

 

4. 调整配置

 

  fckeditor.properties

# allow client upload file to server
connector.userActionImpl=net.fckeditor.requestcycle.impl.EnabledUserAction

# user can see path
connector.userFilesPath=/userfiles/fckeditor

# user can store path
connector.userFilesAbsolutePath=/userfiles/fckeditor

# the default path of the editor
fckeditor.basePath=/plugins/fckeditor

 

fckconfig.js

FCKConfig.AutoDetectLanguage	= false ;
FCKConfig.DefaultLanguage		= 'zh-cn' ;

// 可以加入自定义的工具栏
// custom your Toolbar and use it
FCKConfig.ToolbarSets["Custom"] = [
  ['Bold','Italic','-','OrderedList','UnorderedList','-','Link','Unlink']
] ;

 

5. 使用

    fckeditor.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.fckeditor.net" prefix="FCK"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>use FCKeditor</title>
</head>
<body>
<form action="commit.jsp" method="post" target="_blank">
<FCK:editor
 instanceName="EditorDefault"
 toolbarSet="Default"
 height="300"
 width="98%"
  >

  <jsp:attribute name="value">
  This is some <strong>sample text</strong>. 
  You are using <a href="http://www.fckeditor.net">FCKeditor</a>.
  </jsp:attribute>
</FCK:editor>

</body>
</html>

 

   commit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String content = request.getParameter("EditorDefault");
%>
<div>
<%= content%>
</div>

 

6. 关于中文文件名问题, 在此重写代码, 自定义文件名.

  这个fckeditor-java-2.6-bin.tar.gz包中有源码, 获取Dispatcher.java, 建立相应的包名net.fckeditor.connector

  调整代码如下:

 

UploadResponse doPost(final HttpServletRequest request) {
    logger.debug("Entering Dispatcher#doPost");

    Context context = ThreadLocalData.getContext();
    context.logBaseParameters();

    UploadResponse uploadResponse = null;
    // check permissions for user actions
    if (!RequestCycleHandler.isFileUploadEnabled(request))
      uploadResponse = UploadResponse.getFileUploadDisabledError();
    // check parameters
    else if (!Command.isValidForPost(context.getCommandStr()))
      uploadResponse = UploadResponse.getInvalidCommandError();
    else if (!ResourceType.isValidType(context.getTypeStr()))
      uploadResponse = UploadResponse.getInvalidResourceTypeError();
    else if (!UtilsFile.isValidPath(context.getCurrentFolderStr()))
      uploadResponse = UploadResponse.getInvalidCurrentFolderError();
    else {

      // call the Connector#fileUpload
      ResourceType type = context.getDefaultResourceType();
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      try {
        List<FileItem> items = upload.parseRequest(request);
        // We upload just one file at the same time
        FileItem uplFile = items.get(0);
        // Some browsers transfer the entire source path not just the
        // filename
        String fileName = FilenameUtils.getName(uplFile.getName());

        // rename file name
        logger.debug("old file name: " + fileName);
        fileName = dateFmt(new Date(), "yyyyMMdd") + "_" + UUID.randomUUID().toString().replace("-", "") + "." + getExtension(fileName);
        logger.debug("rename file name: " + fileName);



        logger.debug("Parameter NewFile: {}", fileName);
        // check the extension
        if (type.isDeniedExtension(FilenameUtils.getExtension(fileName)))
          uploadResponse = UploadResponse.getInvalidFileTypeError();
        // Secure image check (can't be done if QuickUpload)
        else if (type.equals(ResourceType.IMAGE) && PropertiesLoader.isSecureImageUploads() && !UtilsFile.isImage(uplFile.getInputStream())) {
          uploadResponse = UploadResponse.getInvalidFileTypeError();
        } else {
          String sanitizedFileName = UtilsFile.sanitizeFileName(fileName);

          logger.debug("Parameter NewFile (sanitized): {}", sanitizedFileName);
          String newFileName = connector.fileUpload(type, context.getCurrentFolderStr(), sanitizedFileName, uplFile.getInputStream());
          String fileUrl = UtilsResponse.fileUrl(RequestCycleHandler.getUserFilesPath(request), type, context.getCurrentFolderStr(), newFileName);

          if (sanitizedFileName.equals(newFileName))
            uploadResponse = UploadResponse.getOK(fileUrl);
          else {
            uploadResponse = UploadResponse.getFileRenamedWarning(fileUrl, newFileName);
            logger.debug("Parameter NewFile (renamed): {}", newFileName);
          }
        }

        uplFile.delete();
      } catch (InvalidCurrentFolderException e) {
        uploadResponse = UploadResponse.getInvalidCurrentFolderError();
      } catch (WriteException e) {
        uploadResponse = UploadResponse.getFileUploadWriteError();
      } catch (IOException e) {
        uploadResponse = UploadResponse.getFileUploadWriteError();
      } catch (FileUploadException e) {
        uploadResponse = UploadResponse.getFileUploadWriteError();
      }
    }

    logger.debug("Exiting Dispatcher#doPost");
    return uploadResponse;
  }


/**
   * get extend of file name
   * 
   * @param fileName
   * @return
   */
  private String getExtension(String fileName) {
    return fileName.substring(fileName.lastIndexOf(".") + 1);
  }



  /**
   * format Date to String
   * 
   * @param mydate
   * @param strDate
   * @return
   */
  private String dateFmt(Date mydate, String strDate) {
    if (mydate == null) {
      return "";
    }
    java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(strDate);
    return df.format(mydate);
  }


 

7. 此时日志有info信息输出,控制日志输出.

    删除lib目录下的: slf4j-simple-1.5.8.jar

 

    同时添加:

      slf4j-log4j12-1.5.8.jar

      log4j-1.2.16.jar

 

    在sample\src中放入(eclipse自动编译后会到classes目录下)log4j.properties

 

log4j.rootLogger=error, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=(%F:%L) - %m%n

 

 

代码中的颜色不知道怎么设置, 没标出具体区别, 见谅.