KindEditor.ready(function(K) { K.each({ 'plug-align' : { name : '对齐方式', method : { 'justifyleft' : '左对齐', 'justifycenter' : '居中对齐', 'justifyright' : '右对齐' } }, 'plug-order' : { name : '编号', method : { 'insertorderedlist' : '数字编号', 'insertunorderedlist' : '项目编号' } }, 'plug-indent' : { name : '缩进', method : { 'indent' : '向右缩进', 'outdent' : '向左缩进' } } },function( pluginName, pluginData ){ var lang = {}; lang[pluginName] = pluginData.name; KindEditor.lang( lang ); KindEditor.plugin( pluginName, function(K) { var self = this; self.clickToolbar( pluginName, function() { var menu = self.createMenu({ name : pluginName, width : pluginData.width || 100 }); K.each( pluginData.method, function( i, v ){ menu.addItem({ title : v, checked : false, iconClass : pluginName+'-'+i, click : function() { self.exec(i).hideMenu(); } }); }) }); }); }); editor = K.create('#nr', { cssPath : '${ctx}/static/kindeditor/plugins/code/prettify.css', uploadJson : '${ctx}/upload/upload/', fileManagerJson : '${ctx}/upload/manageImg', allowFileManager : true, themeType : 'default', filterMode : true, items :[ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'clearhtml', 'quickformat', 'selectall', '|', 'formatblock', 'fontname', 'fontsize', '|','/', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', '|', 'image', 'multiimage' ] , afterBlur:function(){this.sync(); } }); });
后台代码
import com.jfinal.core.Controller; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Random; import org.apache.commons.io.FileUtils; import com.alibaba.fastjson.JSON; import com.jfinal.core.JFinal; import com.jfinal.render.JsonRender; import com.jfinal.upload.UploadFile; /** * Created with IntelliJ IDEA. * User: xiaojing * Date: 14-8-2 * Time: 下午4:40 * To change this template use File | Settings | File Templates. */ public class UploadController extends Controller { /** * 文件上传 * @throws Exception */ public void upload() throws Exception{ //定义允许上传的文件扩展名 HashMap<String, String> extMap = new HashMap<String, String>(); extMap.put("image", "gif,jpg,jpeg,png,bmp"); extMap.put("flash", "swf,flv"); extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"); extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,pdf"); Map result = new HashMap(); String dirName = getPara("dir")==null?"image":getPara("dir"); String realpath = getRequest().getRealPath("/uploadfile"); UploadFile uf= getFile("imgFile",realpath); String affix_id = ""; String affix_name = ""; if(uf!=null){ affix_name = uf.getFile().getName(); File file = uf.getFile(); //检查扩展名 String fileExt = affix_name.substring(affix_name.lastIndexOf(".") + 1).toLowerCase(); if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){ result.put("error", 1); result.put("message", "上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。"); file.delete(); }else{ SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); affix_id = df.format(new Date()) + "_" + new Random().nextInt(1000)+"."+fileExt; File savefile = new File(new File(realpath),affix_id); FileUtils.copyFile(file, savefile); if(file.exists()){ file.delete(); } result.put("error", 0); result.put("url", JFinal.me().getContextPath()+"/uploadfile/"+affix_id); } }else{ result.put("error", 1); result.put("message", "请选择文件"); } render(new JsonRender(JSON.toJSONString(result)).forIE()); } /** * 文件管理 */ public void manageImg(){ String[] fileTypes = new String[]{"gif", "jpg", "jpeg", "png", "bmp"}; String currentPath = getRequest().getRealPath("/uploadfile")+"/"; File currentPathFile = new File(currentPath); List<Hashtable> fileList = new ArrayList<Hashtable>(); if(currentPathFile.listFiles() != null) { for (File file : currentPathFile.listFiles()) { Hashtable<String, Object> hash = new Hashtable<String, Object>(); String fileName = file.getName(); if(file.isDirectory()) { hash.put("is_dir", true); hash.put("has_file", (file.listFiles() != null)); hash.put("filesize", 0L); hash.put("is_photo", false); hash.put("filetype", ""); } else if(file.isFile()){ String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); hash.put("is_dir", false); hash.put("has_file", false); hash.put("filesize", file.length()); hash.put("is_photo", Arrays.<String>asList(fileTypes).contains(fileExt)); hash.put("filetype", fileExt); } hash.put("filename", fileName); hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified())); fileList.add(hash); } } //排序形式,name or size or type String order = getPara("order") != null ? getPara("order").toLowerCase() : "name"; Map result = new HashMap(); result.put("moveup_dir_path", ""); result.put("current_dir_path",""); result.put("current_url", JFinal.me().getContextPath()+"/uploadfile/"); result.put("total_count", fileList.size()); result.put("file_list", fileList); System.out.println(JSON.toJSONString(result)); renderJson(JSON.toJSONString(result)); } }