国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
CKEDITOR 通過SERVLET 打開上傳功能
在CKEditor中把上傳配置給打開,很簡單,腳本段改為如下設(shè)置:
  1. <script type="text/javascript">  
  2.         CKEDITOR.replace('content',{filebrowserUploadUrl : '/ckeditor/ckeditor/uploader?Type=File',  
  3. filebrowserImageUploadUrl : '/ckeditor/ckeditor/uploader?Type=Image',  
  4. filebrowserFlashUploadUrl : '/ckeditor/ckeditor/uploader?Type=Flash'  
  5.         });  
  6. </script>  

    這里參數(shù)我們可以自己設(shè)置,加個Type為了區(qū)分文件類型,因為都使用同一個Servlet處理。事情沒有這么簡單,CKEditor畢竟是個復(fù)雜的組件,我們這么配置,看看它給我們還原成什么了吧,在FireFox中使用FireBug查看,看到了這些:

    看到了吧,在Type后面它為我們又掛接了幾個參數(shù),其中我們需要的是CKEditorFuncNum和file域的name值 upload,CKEditorFuncNum這個參數(shù)是用來回調(diào)頁面的,就是上傳成功后,頁面自動切換到“圖像”選項卡。upload參數(shù)是 servlet獲取上傳文件用的參數(shù)名。其余參數(shù)就根據(jù)需要進行了。
    這些參數(shù)的名稱都是查看源碼獲得的,不能想當(dāng)然。有了這些東西后面就好辦了,就是文件上傳了么。很簡單了。這里我們使用apache commons組件中的fileupload和io。
先看web.xml,我們做些設(shè)置。
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.   
  7.     <servlet>  
  8.         <servlet-name>SimpleUploader</servlet-name>  
  9.         <servlet-class>ckeditor.CKEditorUploadServlet</servlet-class>  
  10.         <init-param>  
  11.             <param-name>baseDir</param-name>  
  12.             <param-value>/UserFiles/</param-value>  
  13.         </init-param>  
  14.         <init-param>  
  15.             <param-name>debug</param-name>  
  16.             <param-value>false</param-value>  
  17.         </init-param>  
  18.         <init-param>  
  19.             <param-name>enabled</param-name>  
  20.             <param-value>true</param-value>  
  21.         </init-param>  
  22.         <init-param>  
  23.             <param-name>AllowedExtensionsFile</param-name>  
  24.             <param-value></param-value>  
  25.         </init-param>  
  26.         <init-param>  
  27.             <param-name>DeniedExtensionsFile</param-name>  
  28.             <param-value>  
  29.                 html|htm|php|php2|php3|php4|php5|phtml|pwml|inc|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|com|dll|vbs|js|reg|cgi|htaccess|asis|ftl  
  30.             </param-value>  
  31.         </init-param>  
  32.         <init-param>  
  33.             <param-name>AllowedExtensionsImage</param-name>  
  34.             <param-value>jpg|gif|jpeg|png|bmp</param-value>  
  35.         </init-param>  
  36.         <init-param>  
  37.             <param-name>DeniedExtensionsImage</param-name>  
  38.             <param-value></param-value>  
  39.         </init-param>  
  40.         <init-param>  
  41.             <param-name>AllowedExtensionsFlash</param-name>  
  42.             <param-value>swf|fla</param-value>  
  43.         </init-param>  
  44.         <init-param>  
  45.             <param-name>DeniedExtensionsFlash</param-name>  
  46.             <param-value></param-value>  
  47.         </init-param>  
  48.         <load-on-startup>0</load-on-startup>  
  49.     </servlet>  
  50.   
  51.     <servlet-mapping>  
  52.         <servlet-name>SimpleUploader</servlet-name>  
  53.         <url-pattern>/ckeditor/uploader</url-pattern>  
  54.     </servlet-mapping>  
  55.   
  56.     <welcome-file-list>  
  57.         <welcome-file>index.html</welcome-file>  
  58.     </welcome-file-list>  
  59. </web-app>  

    主要是Servlet的初始化參數(shù),規(guī)定了文件上傳的擴展名規(guī)則,就是允許上傳的類型和阻止上傳的類型。分為File,Image和FLASH三種,這個上傳參數(shù)的設(shè)置是對應(yīng)的。Debug是設(shè)置servlet知否進行debug,默認是關(guān)閉的。enabled是設(shè)置該servlet是否有效,如果禁止上傳,就打成false。還有一個baseDir是設(shè)定CKEditor上傳文件的存放位置。
    下面就是實現(xiàn)類了,比較長,但是有詳細的注釋:
  1. package ckeditor;  
  2. import java.io.*;  
  3. import java.text.SimpleDateFormat;  
  4. import java.util.*;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.*;  
  7. import org.apache.commons.fileupload.FileItem;  
  8. import org.apache.commons.fileupload.FileItemFactory;  
  9. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
  10. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  11. public class CKEditorUploadServlet extends HttpServlet {  
  12.     private static String baseDir;// CKEditor的根目錄  
  13.     private static boolean debug = false;// 是否debug模式  
  14.     private static boolean enabled = false;// 是否開啟CKEditor上傳  
  15.     private static Hashtable allowedExtensions;// 允許的上傳文件擴展名  
  16.     private static Hashtable deniedExtensions;// 阻止的上傳文件擴展名  
  17.     private static SimpleDateFormat dirFormatter;// 目錄命名格式:yyyyMM  
  18.     private static SimpleDateFormat fileFormatter;// 文件命名格式:yyyyMMddHHmmssSSS  
  19.     /** 
  20.      * Servlet初始化方法 
  21.      */  
  22.     public void init() throws ServletException {  
  23.         // 從web.xml中讀取debug模式  
  24.         debug = (new Boolean(getInitParameter("debug"))).booleanValue();  
  25.         if (debug)  
  26.             System.out  
  27.                     .println("\r\n---- SimpleUploaderServlet initialization started ----");  
  28.         // 格式化目錄和文件命名方式  
  29.         dirFormatter = new SimpleDateFormat("yyyyMM");  
  30.         fileFormatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");  
  31.         // 從web.xml中獲取根目錄名稱  
  32.         baseDir = getInitParameter("baseDir");  
  33.         // 從web.xml中獲取是否可以進行文件上傳  
  34.         enabled = (new Boolean(getInitParameter("enabled"))).booleanValue();  
  35.         if (baseDir == null)  
  36.             baseDir = "/UserFiles/";  
  37.         String realBaseDir = getServletContext().getRealPath(baseDir);  
  38.         File baseFile = new File(realBaseDir);  
  39.         if (!baseFile.exists()) {  
  40.             baseFile.mkdirs();  
  41.         }  
  42.         // 實例化允許的擴展名和阻止的擴展名  
  43.         allowedExtensions = new Hashtable(3);  
  44.         deniedExtensions = new Hashtable(3);  
  45.         // 從web.xml中讀取配置信息  
  46.         allowedExtensions.put("File",  
  47.         stringToArrayList(getInitParameter("AllowedExtensionsFile")));  
  48.         deniedExtensions.put("File",  
  49.         stringToArrayList(getInitParameter("DeniedExtensionsFile")));  
  50.         allowedExtensions.put("Image",  
  51.     stringToArrayList(getInitParameter("AllowedExtensionsImage")));  
  52.         deniedExtensions.put("Image",           stringToArrayList(getInitParameter("DeniedExtensionsImage")));  
  53.         allowedExtensions.put("Flash",          stringToArrayList(getInitParameter("AllowedExtensionsFlash")));  
  54.         deniedExtensions.put("Flash",           stringToArrayList(getInitParameter("DeniedExtensionsFlash")));  
  55.         if (debug)  
  56.             System.out  
  57.                     .println("---- SimpleUploaderServlet initialization completed ----\r\n");  
  58.     }  
  59.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  60.             throws ServletException, IOException {  
  61.         doPost(request, response);  
  62.     }  
  63.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  64.             throws ServletException, IOException {  
  65.         if (debug)  
  66.             System.out.println("--- BEGIN DOPOST ---");  
  67.         response.setContentType("text/html; charset=UTF-8");  
  68.         response.setHeader("Cache-Control", "no-cache");  
  69.         PrintWriter out = response.getWriter();  
  70.         // 從請求參數(shù)中獲取上傳文件的類型:File/Image/Flash  
  71.         String typeStr = request.getParameter("Type");  
  72.         if (typeStr == null) {  
  73.             typeStr = "File";  
  74.         }  
  75.         if (debug)  
  76.             System.out.println(typeStr);  
  77.         // 實例化dNow對象,獲取當(dāng)前時間  
  78.         Date dNow = new Date();  
  79.         // 設(shè)定上傳文件路徑  
  80.         String currentPath = baseDir + typeStr + "/"  
  81.                 + dirFormatter.format(dNow);  
  82.         // 獲得web應(yīng)用的上傳路徑  
  83.         String currentDirPath = getServletContext().getRealPath(currentPath);  
  84.         // 判斷文件夾是否存在,不存在則創(chuàng)建  
  85.         File dirTest = new File(currentDirPath);  
  86.         if (!dirTest.exists()) {  
  87.             dirTest.mkdirs();  
  88.         }  
  89.         // 將路徑前加上web應(yīng)用名  
  90.         currentPath = request.getContextPath() + currentPath;  
  91.         if (debug)  
  92.             System.out.println(currentDirPath);  
  93.         // 文件名和文件真實路徑  
  94.         String newName = "";  
  95.         String fileUrl = "";  
  96.         if (enabled) {  
  97.             // 使用Apache Common組件中的fileupload進行文件上傳  
  98.             FileItemFactory factory = new DiskFileItemFactory();  
  99.             ServletFileUpload upload = new ServletFileUpload(factory);  
  100.             try {  
  101.                 List items = upload.parseRequest(request);  
  102.                 Map fields = new HashMap();  
  103.                 Iterator iter = items.iterator();  
  104.                 while (iter.hasNext()) {  
  105.                     FileItem item = (FileItem) iter.next();  
  106.                     if (item.isFormField())  
  107.                         fields.put(item.getFieldName(), item.getString());  
  108.                     else  
  109.                         fields.put(item.getFieldName(), item);  
  110.                 }  
  111.                 // CEKditor中file域的name值是upload  
  112.                 FileItem uplFile = (FileItem) fields.get("upload");  
  113.                 // 獲取文件名并做處理  
  114.                 String fileNameLong = uplFile.getName();  
  115.                 fileNameLong = fileNameLong.replace('\\', '/');  
  116.                 String[] pathParts = fileNameLong.split("/");  
  117.                 String fileName = pathParts[pathParts.length - 1];  
  118.                 // 獲取文件擴展名  
  119.                 String ext = getExtension(fileName);  
  120.                 // 設(shè)置上傳文件名  
  121.                 fileName = fileFormatter.format(dNow) + "." + ext;  
  122.                 // 獲取文件名(無擴展名)  
  123.                 String nameWithoutExt = getNameWithoutExtension(fileName);  
  124.                 File pathToSave = new File(currentDirPath, fileName);  
  125.                 fileUrl = currentPath + "/" + fileName;  
  126.                 if (extIsAllowed(typeStr, ext)) {  
  127.                     int counter = 1;  
  128.                     while (pathToSave.exists()) {  
  129.                         newName = nameWithoutExt + "_" + counter + "." + ext;  
  130.                         fileUrl = currentPath + "/" + newName;  
  131.                         pathToSave = new File(currentDirPath, newName);  
  132.                         counter++;  
  133.                     }  
  134.                     uplFile.write(pathToSave);  
  135.                 } else {  
  136.                     if (debug)  
  137.                         System.out.println("無效的文件類型: " + ext);  
  138.                 }  
  139.             } catch (Exception ex) {  
  140.                 if (debug)  
  141.                     ex.printStackTrace();  
  142.             }  
  143.         } else {  
  144.             if (debug)  
  145.                 System.out.println("未開啟CKEditor上傳功能");  
  146.         }  
  147.         // CKEditorFuncNum是回調(diào)時顯示的位置,這個參數(shù)必須有  
  148.         String callback = request.getParameter("CKEditorFuncNum");  
  149.         out.println("<script type=\"text/javascript\">");  
  150.         out.println("window.parent.CKEDITOR.tools.callFunction(" + callback  
  151.                 + ",'" + fileUrl + "',''" + ")");  
  152.         out.println("</script>");  
  153.         out.flush();  
  154.         out.close();  
  155.         if (debug)  
  156.             System.out.println("--- END DOPOST ---");  
  157.     }  
  158.     /** 
  159.      * 獲取文件名的方法 
  160.      */  
  161.     private static String getNameWithoutExtension(String fileName) {  
  162.         return fileName.substring(0, fileName.lastIndexOf("."));  
  163.     }  
  164.     /** 
  165.      * 獲取擴展名的方法 
  166.      */  
  167.     private String getExtension(String fileName) {  
  168.         return fileName.substring(fileName.lastIndexOf(".") + 1);  
  169.     }  
  170.     /** 
  171.      * 字符串像ArrayList轉(zhuǎn)化的方法 
  172.      */  
  173.     private ArrayList stringToArrayList(String str) {  
  174.         if (debug)  
  175.             System.out.println(str);  
  176.         String[] strArr = str.split("\\|");  
  177.         ArrayList tmp = new ArrayList();  
  178.         if (str.length() > 0) {  
  179.             for (int i = 0; i < strArr.length; ++i) {  
  180.                 if (debug)  
  181.                     System.out.println(i + " - " + strArr[i]);  
  182.                 tmp.add(strArr[i].toLowerCase());  
  183.             }  
  184.         }  
  185.         return tmp;  
  186.     }  
  187.     /** 
  188.      * 判斷擴展名是否允許的方法 
  189.      */  
  190.     private boolean extIsAllowed(String fileType, String ext) {  
  191.         ext = ext.toLowerCase();  
  192.         ArrayList allowList = (ArrayList) allowedExtensions.get(fileType);  
  193.         ArrayList denyList = (ArrayList) deniedExtensions.get(fileType);  
  194.         if (allowList.size() == 0) {  
  195.             if (denyList.contains(ext)) {  
  196.                 return false;  
  197.             } else {  
  198.                 return true;  
  199.             }  
  200.         }  
  201.         if (denyList.size() == 0) {  
  202.             if (allowList.contains(ext)) {  
  203.                 return true;  
  204.             } else {  
  205.                 return false;  
  206.             }  
  207.         }  
  208.         return false;  
  209.     }  
  210. }  

    只要在頁面中的script中設(shè)置了上傳屬性,我們打開圖片時就能看到上傳選項卡了,選擇圖片后,點擊上傳到服務(wù)器,上傳成功就會自動跳到圖像選項卡,可以看到源文件已經(jīng)存在服務(wù)器的目標目錄中了,此時,我們就可以在編輯器中編輯上傳的圖片了,非常方便。

    下面我們進行圖片上傳測試,可以看到如下效果。

    提交后可以看到,數(shù)據(jù)獲得效果,是完全一致的,這樣使用CKEditor上傳文件就已經(jīng)成功了。

    我們查看源文件,得到如下結(jié)果。
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Display Content</title>  
  5. </head>  
  6. <body>  
  7. <center>  
  8. <table width="600" border="0" bordercolor="000000"  
  9.     style="table-layout: fixed;">  
  10.     <tbody>  
  11.         <tr>  
  12.             <td width="100" bordercolor="ffffff">主題:</td>  
  13.             <td width="500" bordercolor="ffffff">圖片上傳測試</td>  
  14.         </tr>  
  15.         <tr>  
  16.             <td valign="top" bordercolor="ffffff">內(nèi)容:</td>  
  17.             <td valign="top" bordercolor="ffffff">  
  18.             <p style="text-align: center;"><span style="color: #f00;"><strong><span  
  19.                 style="font-family: courier new, courier, monospace;"><span  
  20.                 style="font-size: 48px;">圖片上傳測試</span></span></strong></span></p>  
  21.             <p style="text-align: center;"><img alt=""  
  22.                 src="/ckeditor/UserFiles/Image/201002/20100217232748000.gif"  
  23.                 style="width: 133px; height: 41px;"></p>  
  24.             <p style="text-align: center;"><span  
  25.                 style="font-family: courier new, courier, monospace;"><br>  
  26.             </span></p>  
  27.             </td>  
  28.         </tr>  
  29.     </tbody>  
  30. </table>  
  31. </center>  
  32. </body>  
  33. </html>  

    在服務(wù)器目錄中,上傳的文件已經(jīng)存在其中了。

    歡迎交流,希望對使用者有用。附件中新增本項目的源碼下載。 
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
log4j日志配置
CSDN技術(shù)中心 log4j--新的日志操作方法
基于Maven的Spring + Spring MVC + Mybatis的環(huán)境搭建 | AmazingHarry
在JSP里使用CKEditor和CKFinder
ckeditor3.6.5+ckfinder2.0.2+jsp編輯器配置 支持服務(wù)器瀏覽/上傳圖片、Flash
CKEditor和CKFinder整合實現(xiàn)在線編輯功能
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服