`
fangbiao23
  • 浏览: 41299 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

jsf 利用 fckeditor进行内容编辑和文件上传

阅读更多
 

一、fckeditor 基本解决中文乱码和可运行的更改程序可参考fangbiao23.iteye.com/admin/show/130831

二、fckeditor在jsf中使用的一些相关设置和修改

web.xml

xml 代码
  1. <!-- 在线编辑 -->  
  2.  <servlet>  
  3.  <servlet-name>Connector</servlet-name>  
  4.  <servlet-class>com.fredck.FCKeditor.connector.ConnectorServlet</servlet-class>  
  5.  <init-param>  
  6.   <param-name>baseDir</param-name>  
  7.   <param-value>/EpFbUserFiles/</param-value>  
  8.  </init-param>  
  9.  <init-param>  
  10.   <param-name>debug</param-name>  
  11.   <param-value>true</param-value>  
  12.  </init-param>  
  13.  <load-on-startup>1</load-on-startup>  
  14. </servlet>  
  15.   
  16. <servlet>  
  17.  <servlet-name>SimpleUploader</servlet-name>  
  18.  <servlet-class>com.fredck.FCKeditor.uploader.SimpleUploaderServlet</servlet-class>  
  19.  <init-param>  
  20.   <param-name>baseDir</param-name>  
  21.   <param-value>/EpFbUserFiles/</param-value>  
  22.  </init-param>  
  23.  <init-param>  
  24.   <param-name>debug</param-name>  
  25.   <param-value>true</param-value>  
  26.  </init-param>  
  27.  <init-param>  
  28.   <param-name>enabled</param-name>  
  29.   <param-value>true</param-value>  
  30.  </init-param>  
  31.  <init-param>  
  32.   <param-name>AllowedFileSize</param-name>  
  33.   <param-value>5242880</param-value>  
  34.  </init-param>  
  35.  <init-param>  
  36.   <param-name>AllowedExtensionsFile</param-name>  
  37.   <param-value></param-value>  
  38.  </init-param>  
  39.  <init-param>  
  40.   <param-name>DeniedExtensionsFile</param-name>  
  41.   <param-value>php|php3|php5|phtml|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|dll|reg|cgi</param-value>  
  42.  </init-param>  
  43.  <init-param>  
  44.   <param-name>AllowedImageSize</param-name>  
  45.   <param-value>5242880</param-value>  
  46.  </init-param>  
  47.  <init-param>  
  48.   <param-name>AllowedExtensionsImage</param-name>  
  49.   <param-value>jpg|gif|jpeg|pjpeg|png|bmp</param-value>  
  50.  </init-param>  
  51.  <init-param>  
  52.   <param-name>DeniedExtensionsImage</param-name>  
  53.   <param-value></param-value>  
  54.  </init-param>  
  55.  <init-param>  
  56.   <param-name>AllowedFlashSize</param-name>  
  57.   <param-value>5242880</param-value>  
  58.  </init-param>  
  59.  <init-param>  
  60.   <param-name>AllowedExtensionsFlash</param-name>  
  61.   <param-value>swf|fla</param-value>  
  62.  </init-param>  
  63.  <init-param>  
  64.   <param-name>DeniedExtensionsFlash</param-name>  
  65.   <param-value></param-value>  
  66.  </init-param>  
  67.  <load-on-startup>1</load-on-startup>  
  68. </servlet>  

 

xml 代码
  1. <servlet>  
  2.     <servlet-name>fckEitorUploadServlet</servlet-name>  
  3.     <servlet-class>ep.jsf.upload.FckEitorUploadServlet</servlet-class>  
  4.   </servlet>  
  5.      
  6.   <servlet-mapping>  
  7.     <servlet-name>fckEitorUploadServlet</servlet-name>  
  8.     <url-pattern>/fckEitorUploadServlet</url-pattern>  
  9.   </servlet-mapping>  
  10.        
  11.   <servlet-mapping>  
  12.     <servlet-name>Connector</servlet-name>  
  13.     <url-pattern>/FCKeditor/editor/filemanager/browser/default/connectors/jsp/connector</url-pattern>  
  14.   </servlet-mapping>  
  15.      
  16.   <servlet-mapping>  
  17.     <servlet-name>SimpleUploader</servlet-name>  
  18.     <url-pattern>/FCKeditor/editor/filemanager/upload/simpleuploader</url-pattern>  
  19.   </servlet-mapping>    

upload.jsf

java 代码
  1. <script type="text/javascript">   
  2.     function FCKeditor_OnComplete( editorInstance )    
  3.     {    
  4.         editorInstance.Events.AttachEvent( 'OnBlur', onEditorBlur );   
  5.     }    
  6.        
  7.     function onEditorBlur(){    
  8.         var msgPath = '';   
  9.         var imgMsg = '';   
  10.         var imgs = FCKeditorAPI.GetInstance('EditorDefault').EditorDocument.body.all.tags("img");   
  11.         var j =0;   
  12.         for(var i=0; i < imgs.length; i++){    
  13.             imgMsg = imgs[i].src;   
  14.             imgMsg = imgMsg.substring(imgMsg.indexOf('eppj')-1);   
  15.                
  16.             if(imgMsg.indexOf('msn') == -1){   
  17.                 if(j==0)   
  18.                     msgPath = imgMsg;   
  19.                 else  
  20.                     msgPath = msgPath + '|'+ imgMsg;    
  21.                 j++;   
  22.             }   
  23.         }    
  24.         document.getElementById('fckEditorForm:inputContent').value = FCKeditorAPI.GetInstance('EditorDefault').EditorDocument.body.innerHTML;    
  25.             }   
  26. </script>   
  27.     <h:panelGroup>   
  28.                 <h:outputLabel for="inputContent" value="资讯内容"/>   
  29.                 <f:verbatim>   
  30.                 <FCK:editor id="EditorDefault" basePath="/eppj/FCKeditor/"  
  31.                     skinPath="/eppj/FCKeditor/editor/skins/office2003/"  
  32.                     toolbarSet="Usable"  
  33.                     imageBrowserURL="/eppj/FCKeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/jsp/connector"  
  34.                     linkBrowserURL="/eppj/FCKeditor/editor/filemanager/browser/default/browser.html?Connector=connectors/jsp/connector"  
  35.                     flashBrowserURL="/eppj/FCKeditor/editor/filemanager/browser/default/browser.html?Type=Flash&Connector=connectors/jsp/connector"  
  36.                     imageUploadURL="/eppj/FCKeditor/editor/filemanager/upload/simpleuploader?Type=Image"  
  37.                     linkUploadURL="/eppj/FCKeditor/editor/filemanager/upload/simpleuploader?Type=File"  
  38.                     flashUploadURL="/eppj/FCKeditor/editor/filemanager/upload/simpleuploader?Type=Flash"  
  39.                     width="80%"    
  40.                     height="450">   
  41.                        
  42.                 </FCK:editor>   
  43.                 </f:verbatim>   
  44.                 <h:message for="inputContent"/>   
  45.             </h:panelGroup>   
  46.             <h:inputHidden id="inputContent" value="#{tradeBackManager.tdp.tradeContent}"> </h:inputHidden>  

editor.jsf

 

java 代码
  1. <h:panelGroup>   
  2.                 <h:outputLabel for="inputContent" value="资讯内容"/>   
  3.                 <f:verbatim>   
  4.                 <FCK:editor id="EditorDefault" basePath="/eppj/FCKeditor/"  
  5.                     skinPath="/eppj/FCKeditor/editor/skins/office2003/"  
  6.                     toolbarSet="Usable"  
  7.                     imageBrowserURL="/eppj/FCKeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/jsp/connector"  
  8.                     linkBrowserURL="/eppj/FCKeditor/editor/filemanager/browser/default/browser.html?Connector=connectors/jsp/connector"  
  9.                     flashBrowserURL="/eppj/FCKeditor/editor/filemanager/browser/default/browser.html?Type=Flash&Connector=connectors/jsp/connector"  
  10.                     imageUploadURL="/eppj/FCKeditor/editor/filemanager/upload/simpleuploader?Type=Image"  
  11.                     linkUploadURL="/eppj/FCKeditor/editor/filemanager/upload/simpleuploader?Type=File"  
  12.                     flashUploadURL="/eppj/FCKeditor/editor/filemanager/upload/simpleuploader?Type=Flash"  
  13.                     width="80%"    
  14.                     height="450" >   
  15.                     <%   
  16.                         FacesContext fcg = FacesContext.getCurrentInstance();   
  17.                         DtTradeBackManager dtfl = (DtTradeBackManager) fcg.getApplication().getVariableResolver().resolveVariable(fcg, "tradeBackManager");   
  18.                         out.print(dtfl.getTdp().getTradeContent());   
  19.                      %>   
  20.                 </FCK:editor>   
  21.                 </f:verbatim>   
  22.                 <h:message for="inputContent"/>   
  23.             </h:panelGroup>   
  24.             <h:inputText id="inputContent" value="#{tradeBackManager.tdp.tradeContent}"> </h:inputText>  
 
分享到:
评论
4 楼 fangbiao23 2007-10-25  
是的!!那個是可以用的,不過我沒用,聽說他有一些bug!在使用他時需要做一番修改!
3 楼 mygol 2007-10-24  
有个fck-faces可以作jsf应用,不过它是基于myfaces的,不知道有没有高人可以改成基于jsf-ri
2 楼 fangbiao23 2007-10-19  
以上提供的只是个人经验所得,仅供参考
1 楼 fangbiao23 2007-10-19  
文件上传SimpleuploadServlet.java
支持文件大小控制
/*
* FCKeditor - The text editor for internet
* Copyright (C) 2003-2005 Frederico Caldeira Knabben
*
* Licensed under the terms of the GNU Lesser General Public License:
* http://www.opensource.org/licenses/lgpl-license.php
*
* For further information visit:
* http://www.fckeditor.net/
*
* File Name: SimpleUploaderServlet.java
* Java File Uploader class.
*
* Version:  2.3
* Modified: 2005-08-11 16:29:00
*
* File Authors:
* Simone Chiaretta (simo@users.sourceforge.net)
*/

package com.fredck.FCKeditor.uploader;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


import org.apache.commons.fileupload.*;

import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.log4j.Logger;

/**
* Servlet to upload files.<br>
*
* This servlet accepts just file uploads, eventually with a parameter specifying file type
*
* @author Simone Chiaretta (simo@users.sourceforge.net)
*/

public class SimpleUploaderServlet extends HttpServlet {

private static String baseDir;
private static boolean debug=false;
private static boolean enabled=false;
private static Hashtable allowedExtensions;
private static Hashtable deniedExtensions;
private static Hashtable allowedSize;//限制文件大小
private static String retVal="0";
private static long typeAllowedSize;
private static Logger log = Logger.getLogger(SimpleUploaderServlet.class);

/**
* Initialize the servlet.<br>
* Retrieve from the servlet configuration the "baseDir" which is the root of the file repository:<br>
* If not specified the value of "/UserFiles/" will be used.<br>
* Also it retrieve all allowed and denied extensions to be handled.
*
*/
public void init() throws ServletException {

debug=(new Boolean(getInitParameter("debug"))).booleanValue();

if(debug) System.out.println("\r\n---- SimpleUploaderServlet initialization started ----");

baseDir=getInitParameter("baseDir");
enabled=(new Boolean(getInitParameter("enabled"))).booleanValue();

if(baseDir==null)
baseDir="/EpUserFiles/";
String realBaseDir=getServletContext().getRealPath(baseDir);
File baseFile=new File(realBaseDir);
if(!baseFile.exists()){
baseFile.mkdir();
}

allowedExtensions = new Hashtable(3);
deniedExtensions = new Hashtable(3);
allowedSize = new Hashtable(3);

allowedExtensions.put("File",stringToArrayList(getInitParameter("AllowedExtensionsFile")));
deniedExtensions.put("File",stringToArrayList(getInitParameter("DeniedExtensionsFile")));
allowedSize.put("File", new Long(getInitParameter("AllowedFileSize")).longValue());

allowedExtensions.put("Image",stringToArrayList(getInitParameter("AllowedExtensionsImage")));
deniedExtensions.put("Image",stringToArrayList(getInitParameter("DeniedExtensionsImage")));
allowedSize.put("Image", new Long(getInitParameter("AllowedImageSize")).longValue());

allowedExtensions.put("Flash",stringToArrayList(getInitParameter("AllowedExtensionsFlash")));
deniedExtensions.put("Flash",stringToArrayList(getInitParameter("DeniedExtensionsFlash")));
allowedSize.put("Flash", new Long(getInitParameter("AllowedFlashSize")).longValue());

if(debug) System.out.println("---- SimpleUploaderServlet initialization completed ----\r\n");

}


/**
* Manage the Upload requests.<br>
*
* The servlet accepts commands sent in the following format:<br>
* simpleUploader?Type=ResourceType<br><br>
* It store the file (renaming it in case a file with the same name exists) and then return an HTML file
* with a javascript command in it.
*
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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;
//在此为每个能上传图片的用片增加一个user目录,利用session来做
currentPath = currentPath+"/userName/";
String currentDirPath=getServletContext().getRealPath(currentPath);
currentPath=request.getContextPath()+currentPath;

File addUserNameFile=new File(currentDirPath);
if(!addUserNameFile.exists()){
addUserNameFile.mkdirs();
}

if (debug) System.out.println(currentDirPath);


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];
long uploadFileSize = uplFile.getSize();

log.info("上传的文件大小为: ["+uploadFileSize+"]");
log.info("上传的文件名称为:["+fileName+"]");

String nameWithoutExt=getNameWithoutExtension(fileName);
String ext=getExtension(fileName);

File pathToSave=new File(currentDirPath,fileName);
//fileUrl=currentPath+"/"+fileName;
fileUrl=currentPath;
if(extIsAllowed(typeStr,ext,uploadFileSize)) {
//确保文件目录存在
if(!addUserNameFile.exists()){
addUserNameFile.mkdirs();
}
int counter=1;
newName = fileName;
while(pathToSave.exists()){
newName=nameWithoutExt+"("+counter+")"+"."+ext;
//fileUrl=currentPath+"/"+newName;
fileUrl=currentPath;
retVal="201";
pathToSave=new File(currentDirPath,newName);
counter++;
}
uplFile.write(pathToSave);
}
else {
//执行size过大提示信息
if(!retVal.equals("88")){
retVal="202";
errorMessage="";
if (debug) System.out.println("无效的文件类型: " + ext);
}else{
errorMessage="上传文件过大,该文件类型上传的大小允许在 "+typeAllowedSize+" 字节范围";
log.info("上传文件名称为:["+fileName+"]的文件大小为: ["+uploadFileSize+"] 超过 {"+typeAllowedSize+"}的允许范围!");
if (debug) System.out.println("上传文件过大: " + ext);
}
}
}catch (Exception ex) {
if (debug) ex.printStackTrace();
retVal="203";
}
}
else {
retVal="1";
errorMessage="无法上传文件. 请检查 WEB-INF/web.xml 文件!";
}


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 ---");

}


/*
* This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF bug #991489
*/
  private static String getNameWithoutExtension(String fileName) {
    return fileName.substring(0, fileName.lastIndexOf("."));
    }
   
/*
* This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF bug #991489
*/
private String getExtension(String fileName) {
return fileName.substring(fileName.lastIndexOf(".")+1);
}



/**
* Helper function to convert the configuration string to an ArrayList.
*/

private ArrayList stringToArrayList(String str) {

if(debug) System.out.println(str);
String[] strArr=str.split("\\|");

ArrayList tmp=new ArrayList();
if(str.length()>0) {
for(int i=0;i<strArr.length;++i) {
if(debug) System.out.println(i +" - "+strArr[i]);
tmp.add(strArr[i].toLowerCase());
}
}
return tmp;
}


/**
* Helper function to verify if a file extension is allowed or not allowed.
*/

private boolean extIsAllowed(String fileType, String ext,long size) {

ext=ext.toLowerCase();

ArrayList allowList=(ArrayList)allowedExtensions.get(fileType);
ArrayList denyList=(ArrayList)deniedExtensions.get(fileType);
long allowSize = ((Long)allowedSize.get(fileType)).longValue();
typeAllowedSize = allowSize;
if(allowList.size()==0){
if(denyList.contains(ext)){
return false;
}else{
if(size <= allowSize){
return true;
}
else{
retVal = "88"; //提示文件过大特殊符号
return false;
}
}
}
if(denyList.size()==0){
if(allowList.contains(ext)){
if(size <= allowSize){
return true;
}
else{
retVal = "88";
return false;
}
}
else{
return false;
}
}
return false;
}

}

相关推荐

    JAVA WEB典型模块与项目实战大全

    第15章 在线文件上传和下载(struts 2.x+fileupload)  15.1 在线文件上传和下载模块原理  15.2 文件上传组件fileupload  15.3 初步使用文件上传组件(components-fileupload)  15.4 单文件的上传  15.5 多...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    JAVA上百实例源码以及开源项目源代码

    凯撒加密解密程序 1个目标文件 1、程序结构化,用函数分别实现 2、对文件的加密,解密输出到文件 利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实...

    java开源包1

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包11

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包2

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包3

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包6

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包5

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包10

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包4

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包8

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包7

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包9

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    java开源包101

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

    Java资源包01

    Java文件上传组件 COS FAT文件系统读写类库 fat32-lib fat32-lib 是一个用来读写 FAT 16/32 格式文件系统的纯 Java 类库(纯的)。 Eclipse的HTML格式化插件 Eclipse Tidy Eclipse HTML Tidy 是一款 Eclipse 的...

Global site tag (gtag.js) - Google Analytics