在線HTML文檔編輯器使用入門之圖片上傳與圖片管理的實現


  1 在線HTML文檔編輯器使用入門之圖片上傳與圖片管理的實現:
  2     官方網址: http://kindeditor.net/demo.php
  3 開發步驟:
  4     1.開發中只需要導入選中的文件(通常在 webapp 下,建立 editor 文件夾 )
  5         導入:lang、plugins、themes、kindeditor.js/kindeditor-min.js-->放在editor文件夾下
  6     2.在頁面上引入相關的js&css文件
  7         <!-- 導入Kindeditor相關文件 -->
  8         <script type="text/javascript" src="../../editor/lang/zh_CN.js"></script>
  9         <script type="text/javascript" src="../../editor/kindeditor.js"></script>
 10         <link rel="stylesheet" href="../../editor/themes/default/default.css" />
 11     3.在頁面提供標簽<textarea id="ta" name="ta"></textarea>
 12         KindEditor.ready(function(K) {
 13             window.editor = K.create("#ta");
 14     4.定制工具欄按鈕顯示:
 15         KindEditor.ready(function(K) {
 16             window.editor = K.create("#ta", {
 17                         items: ['source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
 18                             'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
 19                             'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
 20                             'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
 21                             'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
 22                             'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
 23                             'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
 24                             'anchor', 'link', 'unlink', '|', 'about'
 25                 ],allowFileManager:true,
 26                 uploadJson:"../../image_upload.action",
 27                 fileManagerJson:"../../image_manager.action"
 28             });
 29     5.上傳圖片與圖片管理功能實現:
 30         頁面提供對應方法:
 31             allowFileManager、uploadJson、fileManagerJson-->發送請求到后台action處理:
 32         //提供屬性注入
 33         private File imgFile;
 34         private String imgFileFileName;
 35         private String imgFileContentType;
 36         //圖片上傳方法
 37         @Action(value = "image_upload", results = { @Result(name = "success", type = "json") })
 38         public String uploadImg() throws IOException {
 39             System.out.println("file:" + imgFile);
 40             System.out.println("文件名稱:" + imgFileFileName);
 41             System.out.println("文件類型:" + imgFileContentType);
 42             String savePath = ServletActionContext.getServletContext().getRealPath(
 43                     "/upload/");
 44             String saveUrl = ServletActionContext.getRequest().getContextPath()
 45                     + savePath;
 46             // 生成隨即圖片名稱
 47             UUID randomUUID = UUID.randomUUID();
 48             String ext = imgFileFileName
 49                     .substring(imgFileFileName.lastIndexOf("."));
 50             String randomUrl = randomUUID + ext;
 51             // 保存圖片(絕對的路徑和)
 52             FileUtils.copyFile(imgFile, new File(savePath + "/" + randomUrl));
 53             // 返回瀏覽器數據(文件上傳成功了還是失敗了)
 54             Map<String, Object> map = new HashMap<String, Object>();
 55             map.put("error", 0);
 56             map.put("url", saveUrl + randomUrl);
 57             ServletActionContext.getContext().getValueStack().push(map);
 58             return SUCCESS;
 59         }
 60 
 61         //圖片管理方法
 62         @Action(value = "image_manager", results = { @Result(name = "success", type = "json") })
 63         public String manager() {
 64             // 根目錄路徑(絕對路徑)
 65             String rootPath = ServletActionContext.getServletContext().getRealPath(
 66                     "/")
 67                     + "upload/";
 68             // 根目錄URL(絕對路徑)
 69             String rootUrl = ServletActionContext.getRequest().getContextPath()
 70                     + "/upload/";
 71             List<Map<String, Object>> fileList = new ArrayList<Map<String, Object>>();
 72             // 當前上傳目錄
 73             File currentPathFile = new File(rootPath);
 74             // 圖片的擴展名
 75             String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
 76 
 77             if (currentPathFile.listFiles() != null) {
 78                 // 遍歷目錄取的文件信息
 79                 for (File file : currentPathFile.listFiles()) {
 80                     Map<String, Object> hash = new HashMap<String, Object>();
 81                     String fileName = file.getName();
 82                     if (file.isDirectory()) {
 83                         hash.put("is_dir", true);
 84                         hash.put("has_file", (file.listFiles() != null));
 85                         hash.put("filesize", 0L);
 86                         hash.put("is_photo", false);
 87                         hash.put("filetype", "");
 88                     } else if (file.isFile()) {
 89                         String fileExt = fileName.substring(
 90                                 fileName.lastIndexOf(".") + 1).toLowerCase();
 91                         hash.put("is_dir", false);
 92                         hash.put("has_file", false);
 93                         hash.put("filesize", file.length());
 94                         hash.put("is_photo", Arrays.<String> asList(fileTypes)
 95                                 .contains(fileExt));
 96                         hash.put("filetype", fileExt);
 97                     }
 98                     hash.put("filename", fileName);
 99                     hash.put("datetime",
100                             new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file
101                                     .lastModified()));
102                     fileList.add(hash);
103                 }
104             }
105             Map<String, Object> result = new HashMap<String, Object>();
106             result.put("moveup_dir_path", "");
107             result.put("current_dir_path", rootPath);
108             result.put("current_url", rootUrl);
109             result.put("total_count", fileList.size());
110             result.put("file_list", fileList);
111             ActionContext.getContext().getValueStack().push(result);
112             return SUCCESS;
113         }
114                 

 


免責聲明!

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



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