@RequestMapping(value="/kindEditorUpload") public void kindEditorUpload(HttpServletRequest request,HttpServletResponse response){ JSONObject obj = new JSONObject(); try { PropertiesUtil propertiesUtil=new PropertiesUtil(); String ftpPath=propertiesUtil.readValue("FILE_DOWNLOAD_PATH"); // 定義容許上傳的文件擴展名 HashMap<String, String> extMap = new HashMap<String, String>(); extMap.put("image", "gif,jpg,jpeg,png,bmp"); MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> fileMap = mRequest.getFileMap(); String fileName = null; for (Iterator<Map.Entry<String, MultipartFile>> it = fileMap.entrySet().iterator(); it.hasNext();) { Map.Entry<String, MultipartFile> entry = it.next(); MultipartFile mFile = entry.getValue(); fileName = mFile.getOriginalFilename(); System.out.println(fileName); String fileExt = fileName.substring(fileName.lastIndexOf(".")+1); if (!Arrays.<String> asList(extMap.get("image").split(",")).contains(fileExt)) { obj.put("error", 1); obj.put("message", "上傳文件擴展名是不容許的擴展名。\n只容許" + extMap.get("image") + "格式。"); }else{ FileSaveResult result=ftpFileAccess.saveFileToFTP(mFile, getFileName()+"."+fileExt); System.out.println(result.getFileName()+"-----"+result.getFilePath()); obj.put("error", 0); obj.put("url", ftpPath + result.getFilePath()); System.out.println(obj.toString()); returnKindEditorMsg(obj.toString(),request,response); } } } catch (Exception e) { e.printStackTrace(); obj.put("error", 1); obj.put("message", "上傳文件失敗,服務器錯誤!"); returnKindEditorMsg(obj.toString(),request,response); } } private void returnKindEditorMsg(String returnMsg,HttpServletRequest request, HttpServletResponse response){ response.setContentType("text/html;charset=utf-8"); // response.setContentType("application/json; charset=UTF-8"); try { response.getWriter().print(returnMsg); response.getWriter().flush(); } catch (Exception e) { e.printStackTrace(); } }
在IE瀏覽器下響應的ContentType必定要定義爲text/html,否則上傳完圖片後,會打開.json的文件css
前臺部分:html
var path="${pageContext.request.contextPath}"; var editor; KindEditor.ready(function(K) { var url=path+'/wthdxx/kindEditorUpload'; editor=K.create('#activityContent', { resizeType : 0, width : this.width, height : this.height, uploadJson : url, allowFlashUpload:false, allowMediaUpload:false, allowImageRemote:false, cssData: 'body {font-family: "宋體"; font-size: 14px;line-height:14px;}', items : [ 'source', '|', 'fullscreen', 'undo', 'redo', '|', 'preview', 'print', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat','|', 'selectall', '/', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' ], afterCreate : function() { var self = this; K.ctrl(document, 13, function() { self.sync(); document.forms['example'].submit(); }); K.ctrl(self.edit.doc, 13, function() { self.sync(); document.forms['example'].submit(); }); } }); });
<form class="" name="example" method="post"> <span> <i> <textarea id="activityContent" name="content" style="width:800px;height:400px;visibility:hidden;display: block;">KindEditor</textarea> </i> </span> </form>