Fckeditor的配置及使用(全)


http://hi.csdn.net/link.php?url=http://blog.csdn.net%2Fhaoren_e%2Farchive%2F2009%2F10%2F05%2F4634856.aspx

FCKeditor簡介:
              FCKeditor是一個專門使用在網頁上屬於開放源代碼的所見即所得文字編輯器。它志於輕量化,不需要太復雜的安裝步驟即可使用。它可和PHP、 JavaScript、ASP、ASP.NET、ColdFusion、Java、以及ABAP等不同的編程語言相結合。“FCKeditor”名稱中的 “FCK” 是這個編輯器的作者的名字Frederico Caldeira Knabben的縮寫。FCKeditor 相容於絕大部分的網頁瀏覽器.
如果要將fckeditor跑起來,首先需要jar包,還有FCKeditor_2.6.4.1.zip,這些大家都可以到www.fckeditor.net官方網站上下載,這里的需要的jar包我給大家列出來:
   commons-fileupload-1.2.1.jar
   commons-io-1.3.2.jar
   fckeditor-java-core-2.4.jar
   java-core-2.4.jar
   slf4j-api-1.5.2.jar
   slf4j-simple-1.5.2.jar
csdn上還有資源,大家都可以去下載
接下來如何調用fckeditor呢?有兩種方式:通過javascript調用和jsp的自定義的標簽來使用
將解壓后的FckEditor_2.6.3放在web 工程的webroot下面
   然后新建一個html,在其中引入fckeditor文件夾中的fckeditor.js,引入如下:<script type="text/javascript" src="http://ayue05.blog.163.com/blog/fckeditor/fckeditor.js">
   接下來:介紹第一種方法:
    <script type="text/javascript">
      var fckeditor=new FCKeditor('FCKeditor1');//新建一個fckeditor實例
      fckeditor.BasePath="/fckeditor/";//設置編輯器的位置,該位置一定要以/結尾,這個basepath是指 fckeditor文件夾下所有文件的地址,默認值是這個,但是我們一般前面還要加上工程的名字,如/prj/fckeditor/這樣
      fckeditor.Create();//創建並且調用一個fckeditor編輯器
    </script>
   
    第二種方法:通過jsp自定義標簽來完成調用
    可以參考:
  @演示工程fckeditor-java-demo-2.4.war :jsp文件夾--》sample02.html
  @fckeditor-java-2.4文檔
 <%@ taglib="http://java.fckeditor.net"%>
 <FCK:editor instanceName="myEditor" basePath="/fckeditor" value="this is value"></FCK:editor><!--注意這里必須寫value值,並且值不能為空字符串-->
 注意:basePath以/開頭,並且這個/代表當前工程的路徑
完成上述步驟后,即可通過瀏覽器訪問該html文件了,大家就可以看到一個簡單的 fckeditor界面了,但是很多東西還是需要自己配置才能使用的

 首先大家可以去www.fckeditor.net官方網站上下載fckeditor-java-demo-2.4.war然后直接丟到tomcat中,即可在瀏覽器中瀏覽頁面http://localhost:8080/fckeditor-java-demo-2.4/
還要下載FCKeditor_2.6.4.1.zip和fckeditor-java-2.4-bin.zip

配置——使用配置文件
   fckconfig.js即主配置文件,如果我們要修改配置,就需要修改該文件中的屬性,一般我們可以直接修改該配置文件,不過我們不采取這樣的做法,一般我們是新建一個配置文件myconfig。js來覆蓋默認的配置文件
   比如,我們新建了一個myconfig.js然后在里面寫上FCKConfig.AutoDetectLanguage=false; FCKConfig.DefaultLanguage='fr'將原本配置文件中的自動發現語言改為false,並指定默認的語言為法語 fr
  那么當我們寫完配置文件后,如果應用呢?有兩種方法:

  1)在原本的配置文件中fckconfig.js中的 FCKConfig.CustomConfigurationPath=''改為 FCKConfig.CustomConfigurationPath='/fck/myconfig.js'
     注意:這種方法會修改所有的關於fckeditor頁面的屬性
  2)在頁面中來制定。在調用fck的 代碼中添加如下語句:fckeditor.Config["CustomConfigurationsPath"]="/test /myconfig.js";
     注意:這種方法只會修改所在頁面的fckeditor的屬性,一般工程發布的時候不確定在哪,所以test應跟換為 FCKeditor.EditorPath,它指的是     fckeditor文件夾下的editor文件夾
總結一下配置 fckeditor:

  1)直接修改主配置文件,fckconfig.js
  2)定義單獨的配置文件(只需要寫需要修改的配置項)
  3)在頁面的代碼中對FCKeditor的實例進行配置
配置加載順序:
  1)加載主配置文件fckconfig.js
  2)加載自定義的配置文件(如果有),覆蓋相同的配置項(注意是相同的)
  3)使用對實例的配置覆蓋相同的配置項(只對當前實例有效)
注意事項:
   1.永遠都不要刪除主配置文件:fckconfig.js
   2.系統會自動偵測並運行適當的界面語言(默認,可以修改)
   3.修改配置后要清空瀏覽器緩存,以免影響結果(或訪問時強制刷新也可以)

一般需要修改的配置
1)自定義ToolbarSet,去掉一些功能
2)加上幾種常用的字體
3)修改“回車”和 “shift+回車”的換行行為:原本fck的回車鍵的效果是換段落,而shift+回車的效果是換行,所以我們要將這兩種效果顛倒一下
4)修改編輯區樣式文件
5)更換表情圖片

關 於設置ToolbarSet:首先在fckconfig.js找到FCKConfig.ToolbarSets[]中括號中可以指定 toolbar的名字,比如mytoolbar,然后在調用fck的頁面設置一下toolbarset為mytoolbar即 fckeditor.ToolbarSet="mytoolbar",這樣就可以了

關於加上幾種字體:只需要FCKConfig.FontNames中添加我們想添加的字體,注意它會提示不讓保存,則設置myconfig.js文件properties的resources選項中的text file encoding ,只能設置為utf-8


關於修改回車換行換段:
FCKConfig.ShiftEnterMode = 'p' ; // p | div | br
FCKConfig.EnterMode = 'br' ;  // p | div | br
改成這樣,原本是將兩行互相調換的

關於編輯區樣式文件:在主配置文件fckconfig.js文件中,找到FCKConfig.EditorAreaCSS = FCKConfig.BasePath + 'css/fck_editorarea.css' ;
我們會發現所有的樣式都是在editor文件夾下的css文件夾下的fck_editorarea.css文件中的,

關於添加自定義表情:
FCKConfig.SmileyPath = FCKConfig.BasePath + 'images/smiley/msn/' ;//表示表情圖片所在文件夾
FCKConfig.SmileyImages = ['regular_smile.gif','sad_smile.gif','wink_smile.gif','teeth_smile.gif','confused_smile.gif','tounge_smile.gif','embaressed_smile.gif','omg_smile.gif','whatchutalkingabout_smile.gif','angry_smile.gif','angel_smile.gif','shades_smile.gif','devil_smile.gif','cry_smile.gif','lightbulb.gif','thumbs_down.gif','thumbs_up.gif','heart.gif','broken_heart.gif','kiss.gif','envelope.gif'] ;//文件夾中的每幅圖片,其內容是個數組
FCKConfig.SmileyColumns = 8 ;//表示每列顯示的表情圖片數
FCKConfig.SmileyWindowWidth= 320 ;//表示彈出的對話框的寬度
FCKConfig.SmileyWindowHeight= 210 ;//表示彈出的對話框的高度

如果我們想將自己的一副圖片加入到表情中,則首先將圖片copy到文件夾中,然后再數組中加入該圖片的名字,即可
  也可以將自己的一個圖片文件夾作為表情圖片文件夾,則需要指定SmileyPath為我們自己的圖片文件夾,然后將文件夾中每個圖片名稱羅列在數組中
這時我們可能遇到,如果圖片太多,則會顯示的很長很長,即使我們設置了SmileyWindowWidth和SmileyWindowHeight也沒有用,這時我們右擊,查看其源文件,應該是http://localhost:8080/fck/fckeditor/editor/dialog/fck_smiley.html這 個html,然后我們找到打開,window.onload事件中dialog.SetAutoSize(true) ;我們將它改為false,這樣就會按照我們指定的寬高來顯示了,可是會少了很多圖片表情,所以我們還要在<body style="overflow: hidden">這里,將hidden改為scroll,如果圖片過多在指定的寬高無法顯示,則會自動添加滾動條

最后提示、:FCKConfig.BasePath和調用fckeditor時指定的BasePath(FCKeditor BathPath)不是同一個,其值也不一樣

文件上傳:這是很常用的
我們可以參照 D:\JAVA\FCKeditor\fckeditor-java-2.4-bin\fckeditor-java-2.4\site \connector.html,該文件,

第一步:
Declare the ConnectorServlet in your web.xml
  <web-app version="2.4">
    <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>
  </web-app>
配置文件

第二步:
在工程的src下新建一個fckeditor.properties文件,文件內容如下:
connector.userActionImpl=net.fckeditor.requestcycle.impl.UserActionImpl

這樣就可以簡單使用上傳了

在上傳文件名為中文的時候會出現名稱會出現亂碼,如何解決?
首先要知道這個亂碼可能是提交的內容是否出現不正確的編碼問題,或者是提交至服務器端處理時出現的問題
首 先我們找到對應的 editor->filemanager->default->frmupload.html我們發現它是以utf-8的編碼顯示的, 沒有任何問題,接着我們看愛你給哪個文件處理的,就在我們剛配置的web.xml文件中已經指定了 net.fckeditor.connector.ConnectorServlet,是這個,這個servlet在referenced libraries下面的fckeditor-java-core。jar包下的net。fckeditor。connector下的servlet,然 后我們要attach source,關聯上源碼,源碼是fckeditor-java-2.4-src.zip,完了之后,按ctrl+o,然后看它的dopost方法,在 List<FileItem> item=upload.parseRequest(request);這里是需要指定編碼的。但這個文件我們修改不了,所以要復制過來,在src下新建 一個package,新建同樣名稱的servlet,然后粘貼,在try之前寫上:upload.setHeaderEncoding("utf- 8");接着必須要在web.xml文件中修改
 <servlet-class>
          net.fckeditor.connector.ConnectorServlet修改這里,將前面的包名給改了
 </servlet-class>

除此之外,還會在創建中文目錄名時出現亂碼
仍然在ConnectorServlet文件中在doget方法中,找到 NewFoldName,然后添上:
String tempstr=request.getParameter("NowFoldName");
tempstr=new String(temstr.getBytes("iso8859-1"),"utf-8");
String newFoldStr=……(tempstr)

引用中文名稱的圖片不能正常顯示?
方法一:修改tomcat連接器的配置:tomcat6.0-->conf-->server.xml-->
<Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
在這個后面增加一個屬性:URIEncoding="utf-8"
修改完后,需要重新啟動 tomcat服務器,不過該種方法不推薦使用

方法二:避免使用中文名稱的圖片,即在對中文名稱的圖片進行保存時,讓其不以中文形式保存,改以uuid編碼方式
在 ConnectorServlet文件中,dopost方法中找到保存文件的地方,pathToSave然后添加如下:
filename=UUID.randomUUID().toString()+"."+extension;

以上講述比較理論,大家可以我已經配置好的ConnectorServlet類,代碼如下:

  1. package net;  
  2.   
  3. import java.io.*;     
  4. import javax.servlet.*;     
  5. import javax.servlet.http.*;     
  6. import java.util.*;     
  7. import java.util.regex.Pattern;     
  8.      
  9. import org.apache.commons.fileupload.*;     
  10.      
  11. import javax.xml.parsers.*;     
  12. import org.w3c.dom.*;     
  13.      
  14. import javax.xml.transform.*;     
  15. import javax.xml.transform.dom.DOMSource;     
  16. import javax.xml.transform.stream.StreamResult;     
  17.      
  18. public class ConnectorServlet extends HttpServlet {     
  19.      
  20.     private static String baseDir;     
  21.      
  22.     private static boolean debug = false;     
  23.      
  24.     // 允許上傳的Image類型的文件類型      
  25.      
  26.     private static String allowedType;     
  27.      
  28.     // 不允許上傳的File類型的文件類行      
  29.      
  30.     private static String unallowedFileType;     
  31.      
  32.     /**   
  33.      * 初始化 servlet.     
  34.      * Retrieve from the servlet configuration the "baseDir" which is the root   
  35.      * of the file repository:     
  36.      * If not specified the value of "/UserFiles/" will be used.   
  37.      *    
  38.      */     
  39.     public void init() throws ServletException {     
  40.         baseDir = getInitParameter("baseDir");     
  41.         debug = (new Boolean(getInitParameter("debug"))).booleanValue();     
  42.         // 設置baseDir的默認值      
  43.      
  44.         if (baseDir == null)     
  45.             baseDir = "/UserFiles/";     
  46.         String realBaseDir = getServletContext().getRealPath(baseDir);     
  47.         File baseFile = new File(realBaseDir);     
  48.         // 建立目錄      
  49.         if (!baseFile.exists()) {     
  50.             baseFile.mkdir();     
  51.         }     
  52.         // 得到允許上傳的Image類型,從web.xml配置      
  53.      
  54.         allowedType = "|jpg|gif|jpeg|png|bmp|";     
  55.         unallowedFileType = "abc";     
  56.     }     
  57.      
  58.     /**   
  59.      * doGet方法處理 (GetFolders, GetFoldersAndFiles, CreateFolder).   
  60.      * 此servlet接收如下參數     
  61.      * connector?Command=CommandName&Type=ResourceType&CurrentFolder=FolderPath   
  62.      * 它最后返回xml   
  63.      */     
  64.     public void doGet(HttpServletRequest request, HttpServletResponse response)     
  65.             throws ServletException, IOException {     
  66.      
  67.         response.setContentType("text/xml; charset=UTF-8");     
  68.         response.setHeader("Cache-Control", "no-cache");     
  69.         PrintWriter out = response.getWriter();     
  70.      
  71.         String commandStr = request.getParameter("Command");     
  72.         // 得到文件類型,Image,File,Flash,Media      
  73.         String typeStr = request.getParameter("Type");     
  74.         // 得到當前的目錄      
  75.      
  76.         String currentFolderStr = request.getParameter("CurrentFolder");     
  77.         // 得到當前目錄的真實路徑      
  78.      
  79.         String currentPath = baseDir + typeStr + currentFolderStr;     
  80.         String currentDirPath = getServletContext().getRealPath(currentPath);     
  81.         // 創建當前路徑      
  82.         File currentDir = new File(currentDirPath);     
  83.         if (!currentDir.exists()) {     
  84.             currentDir.mkdir();     
  85.         }     
  86.         // 創建返回的xml文件      
  87.         Document document = null;     
  88.         try {     
  89.             DocumentBuilderFactory factory = DocumentBuilderFactory     
  90.                     .newInstance();     
  91.             DocumentBuilder builder = factory.newDocumentBuilder();     
  92.             document = builder.newDocument();     
  93.         } catch (ParserConfigurationException pce) {     
  94.             pce.printStackTrace();     
  95.         }     
  96.      
  97.         Node root = CreateCommonXml(document, commandStr, typeStr,     
  98.                 currentFolderStr, request.getContextPath() + currentPath);     
  99.         // 得到用戶選擇的目錄      
  100.      
  101.         if (commandStr.equals("GetFolders")) {     
  102.             getFolders(currentDir, root, document);     
  103.         }     
  104.         // 得到用戶選擇的目錄的文件      
  105.         else if (commandStr.equals("GetFoldersAndFiles")) {     
  106.             getFolders(currentDir, root, document);     
  107.             getFiles(currentDir, root, document);     
  108.         }     
  109.         // 新建的目錄      
  110.      
  111.         else if (commandStr.equals("CreateFolder")) {     
  112.             String newFolderStr = request.getParameter("NewFolderName");    
  113.             //上傳時,創建文件夾時出現亂碼問題的解決方法  
  114.             newFolderStr=new String(newFolderStr.getBytes("iso8859-1"),"UTF-8");  
  115.             File newFolder = new File(currentDir, newFolderStr);    
  116.             System.out.println(newFolderStr+"文件夾");  
  117.             String retValue = "110";     
  118.              
  119.             // 只能輸入英文字符數字或下划線      
  120.             if (!(Pattern.matches("\\w+", newFolderStr))) {     
  121.                 retValue = "102";     
  122.             }     
  123.             // 目錄已經存在      
  124.             else if (newFolder.exists()) {     
  125.                 retValue = "101";     
  126.             }     
  127.             // 建立新目錄      
  128.      
  129.             else {     
  130.                 try {     
  131.                     boolean dirCreated = newFolder.mkdir();     
  132.                     if (dirCreated)     
  133.                         retValue = "0";     
  134.                     else     
  135.                         retValue = "102";     
  136.                 } catch (SecurityException sex) {  
  137.                     sex.printStackTrace();  
  138.                     retValue = "103";     
  139.                 }     
  140.      
  141.             }     
  142.             setCreateFolderResponse(retValue, root, document);     
  143.         }     
  144.      
  145.         document.getDocumentElement().normalize();     
  146.         try {     
  147.             TransformerFactory tFactory = TransformerFactory.newInstance();     
  148.             Transformer transformer = tFactory.newTransformer();     
  149.      
  150.             DOMSource source = new DOMSource(document);     
  151.      
  152.             StreamResult result = new StreamResult(out);     
  153.             transformer.transform(source, result);     
  154.      
  155.             if (debug) {     
  156.                 StreamResult dbgResult = new StreamResult(System.out);     
  157.                 transformer.transform(source, dbgResult);     
  158.             }     
  159.      
  160.         } catch (Exception ex) {     
  161. //            ex.printStackTrace();     
  162.         }     
  163.      
  164.         out.flush();     
  165.         out.close();     
  166.     }     
  167.      
  168.     /**   
  169.      * 處理文件上傳     
  170.      *    
  171.      * 此servlet接收如下參數:     
  172.      * connector?Command=FileUpload&Type=ResourceType&CurrentFolder=FolderPath   
  173.      */     
  174.     public void doPost(HttpServletRequest request, HttpServletResponse response)     
  175.             throws ServletException, IOException {     
  176.      
  177.         response.setContentType("text/html; charset=UTF-8");     
  178.         response.setHeader("Cache-Control", "no-cache");     
  179.         PrintWriter out = response.getWriter();     
  180.         String commandStr = request.getParameter("Command");     
  181.         // 得到文件類型,Image,File,Flash,Media      
  182.         String typeStr = request.getParameter("Type");     
  183.         // 得到當前目錄      
  184.         String currentFolderStr = request.getParameter("CurrentFolder");     
  185.         // 得到當前的真實路徑      
  186.      
  187.         String currentPath = baseDir + typeStr + currentFolderStr;     
  188.         String currentDirPath = getServletContext().getRealPath(currentPath);     
  189.      
  190.         String retVal = "0";     
  191.         String newName = "";     
  192.      
  193.         if (!commandStr.equals("FileUpload"))     
  194.             retVal = "203";     
  195.         else {     
  196.             // 產生的上傳對象,並設置編譯      
  197.             DiskFileUpload upload = new DiskFileUpload();     
  198.             upload.setHeaderEncoding("UTF-8");     
  199.             try {     
  200.                 List items = upload.parseRequest(request);     
  201.      
  202.                 Map fields = new HashMap();     
  203.      
  204.                 Iterator iter = items.iterator();     
  205.                 while (iter.hasNext()) {     
  206.                     FileItem item = (FileItem) iter.next();     
  207.                     if (item.isFormField())     
  208.                         fields.put(item.getFieldName(), item.getString());     
  209.                     else     
  210.                         fields.put(item.getFieldName(), item);     
  211.                 }     
  212.                 FileItem uplFile = (FileItem) fields.get("NewFile");     
  213.                 String fileNameLong = uplFile.getName();     
  214.                 fileNameLong = fileNameLong.replace('\\', '/');     
  215.                 String[] pathParts = fileNameLong.split("/");     
  216.                 String fileName = pathParts[pathParts.length - 1];     
  217.                   
  218.                  
  219.                 String ext = getExtension(fileName);     
  220.                 //為防止中文圖片無法正確讀取,在保存前,將圖片名稱改為uuid編碼格式  
  221.                 fileName=UUID.randomUUID().toString()+"."+ext;  
  222.                 String nameWithoutExt = getNameWithoutExtension(fileName);     
  223.                 File pathToSave = new File(currentDirPath, fileName);   
  224.                 System.out.println("pathToSave"+pathToSave);  
  225.                 int counter = 1;     
  226.                 int limitedSize=70*1024;//限制上傳圖片的大小  
  227.                 // 檢查允許上傳的文件類型是否為Image,是則再檢查文件類型在web.xml配置      
  228.                 if ((this.checkImageType(allowedType, fileName))     
  229.                         && (typeStr.equals("Image"))) {     
  230.                     retVal = "202";   
  231. //                    System.out.println("dd");  
  232.                 } else if ((this.checkFileType(unallowedFileType, fileName))     
  233.                         && (typeStr.equals("File"))) {     
  234. //                  System.out.println("ddee");  
  235.                     retVal = "202";   
  236.                     }  
  237.                     //如果文件大小超過限制,則返回一個自定義的錯誤碼  
  238.                     else if(uplFile.getSize()>limitedSize)  
  239.                     {  
  240.                         retVal="204";  
  241.                     }  
  242.                  else {     
  243.                     while (pathToSave.exists())   
  244.                 {     
  245.                         newName = nameWithoutExt + "(" + counter + ")" + "."     
  246.                                 + ext;    
  247.                         newName=UUID.randomUUID().toString()+"."+ext;  
  248.                         retVal = "201";     
  249.                         pathToSave = new File(currentDirPath, newName);     
  250.                         counter++;     
  251.                         System.out.println("namewithoutext:"+nameWithoutExt);  
  252.                     }     
  253.                     uplFile.write(pathToSave);    
  254.                       
  255.                 }     
  256.             } catch (Exception ex) {     
  257.                 ex.printStackTrace();  
  258.                 retVal = "203";     
  259.             }     
  260.      
  261.         }     
  262.      
  263. //        out.println("<SCRIPT type='\"text/javascript\"'>");     
  264.         out.print("<mce:script language='javascript'><!--  
  265. window.parent.frames['frmUpload'].OnUploadCompleted("+retVal + ",'" + newName + "');  
  266. // --></mce:script>");  
  267. //        out.println("window.parent.frames['frmUpload'].OnUploadCompleted("     
  268. //                + retVal + ",'" + newName + "');");     
  269. //        out.println("</SCRIPT>");     
  270. //        out.flush();     
  271. //        out.close();     
  272.     }     
  273.      
  274.     private void setCreateFolderResponse(String retValue, Node root,     
  275.             Document doc) {     
  276.         Element myEl = doc.createElement("Error");     
  277.         myEl.setAttribute("number", retValue);     
  278.         root.appendChild(myEl);     
  279.     }     
  280.      
  281.     private void getFolders(File dir, Node root, Document doc) {     
  282.         Element folders = doc.createElement("Folders");     
  283.         root.appendChild(folders);     
  284.         File[] fileList = dir.listFiles();     
  285.         for (int i = 0; i < fileList.length; ++i) {     
  286.             if (fileList[i].isDirectory()) {     
  287.                 Element myEl = doc.createElement("Folder");     
  288.                 myEl.setAttribute("name", fileList[i].getName());     
  289.                 folders.appendChild(myEl);     
  290.             }     
  291.         }     
  292.     }     
  293.      
  294.     private void getFiles(File dir, Node root, Document doc) {     
  295.         Element files = doc.createElement("Files");     
  296.         root.appendChild(files);     
  297.         File[] fileList = dir.listFiles();     
  298.         for (int i = 0; i < fileList.length; ++i) {     
  299.             if (fileList[i].isFile()) {     
  300.                 Element myEl = doc.createElement("File");     
  301.                 myEl.setAttribute("name", fileList[i].getName());     
  302.                 myEl.setAttribute("size", "" + fileList[i].length() / 1024);     
  303.                 files.appendChild(myEl);     
  304.             }     
  305.         }     
  306.     }     
  307.      
  308.     private Node CreateCommonXml(Document doc, String commandStr,     
  309.             String typeStr, String currentPath, String currentUrl) {     
  310.      
  311.         Element root = doc.createElement("Connector");     
  312.         doc.appendChild(root);     
  313.         root.setAttribute("command", commandStr);     
  314.         root.setAttribute("resourceType", typeStr);     
  315.      
  316.         Element myEl = doc.createElement("CurrentFolder");     
  317.         myEl.setAttribute("path", currentPath);     
  318.         myEl.setAttribute("url", currentUrl);     
  319.         root.appendChild(myEl);     
  320.      
  321.         return root;     
  322.      
  323.     }     
  324.      
  325.     /**   
  326.      * 得到文件名,沒有擴展名   
  327.      *    
  328.      *    
  329.      *    
  330.      * @param fileName   
  331.      * @return   
  332.      */     
  333.     private static String getNameWithoutExtension(String fileName) {     
  334.         return fileName.substring(0, fileName.lastIndexOf("."));     
  335.     }     
  336.      
  337.     /**   
  338.      * 得到文件的擴展名   
  339.      *    
  340.      * @param fileName   
  341.      * @return   
  342.      */     
  343.     private String getExtension(String fileName) {     
  344.         return fileName.substring(fileName.lastIndexOf(".") + 1);     
  345.     }     
  346.      
  347.     /**   
  348.      * 檢查上傳Image類型的的文件類型,根據在 web.xml配置的信息   
  349.      *    
  350.      *    
  351.      *    
  352.      * @param type   
  353.      * @param fileName   
  354.      * @return   
  355.      */     
  356.     private boolean checkImageType(String type, String fileName) {  
  357.         System.out.println(type+"type");  
  358.         String[] ss = type.split("\\|");     
  359.         System.out.println(ss+"ss");  
  360.         if (type.length() > 0) {     
  361.             for (int i = 0; i < ss.length; i++) {     
  362.                 if (this.getExtension(fileName).equalsIgnoreCase(ss[i])) {     
  363.                     return false;     
  364.                 }   
  365. //                System.out.println(ss[i]);  
  366.             }     
  367.         }     
  368.         return true;     
  369.     }     
  370.      
  371.     /**   
  372.      * 驗證不允許上傳的File類型的文件   
  373.      *    
  374.      *    
  375.      * @param type   
  376.      * @param fileName   
  377.      * @return   
  378.      */     
  379.     private boolean checkFileType(String type, String fileName) {     
  380.         String[] ss = type.split("\\|");     
  381.         if (type.length() > 0) {     
  382.             for (int i = 0; i < ss.length; i++) {     
  383.                 if (this.getExtension(fileName).equalsIgnoreCase(ss[i])) {     
  384.                     return true;     
  385.                 }   
  386. //                System.out.println(ss[i]);  
  387. //                System.out.println(fileName);  
  388.             }     
  389.         }     
  390.         return false;     
  391.     }     
  392.      
  393. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM