浏览 7539 次
精华帖 (1) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-10-19
一、fckeditor 基本解决中文乱码和可运行的更改程序可参考fangbiao23.iteye.com/admin/show/130831 二、fckeditor在jsf中使用的一些相关设置和修改 web.xml xml 代码
xml 代码
upload.jsf java 代码
editor.jsf
java 代码
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间: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; } } |
|
返回顶楼 | |
发表时间:2007-10-19
以上提供的只是个人经验所得,仅供参考
|
|
返回顶楼 | |
发表时间:2007-10-24
有个fck-faces可以作jsf应用,不过它是基于myfaces的,不知道有没有高人可以改成基于jsf-ri
|
|
返回顶楼 | |
发表时间:2007-10-25
是的!!那個是可以用的,不過我沒用,聽說他有一些bug!在使用他時需要做一番修改!
|
|
返回顶楼 | |