一、引入jsjavascript
<script src="${base}/scripts/kindeditor/kindeditor-all.js" type="text/javascript"></script>
<script src="${base}/scripts/kindeditor/lang/zh-CN.js" type="text/javascript"></script>html
二、初始化java
<script>
//編輯器
var KE;
KindEditor.ready(function (K) {
KE = K.create('#Contents', {
filePostName: 'imgFile',
resizeType : 1,
allowPreviewEmoticons : true,
allowImageUpload : true, //打開本地上傳功能
allowFileManager : true,
uploadJson: '${base}/worksheet/upload',
afterUpload: function(){this.sync();}, //圖片上傳後,將上傳內容同步到textarea中
afterBlur: function(){this.sync();} ////失去焦點時,將上傳內容同步到textarea中
});
KindEditor.create('#Contents', { allowImageUpload: false, resizeType: 1, width: "600px", height: "300px" });
});
</script>服務器
三、文本域(id爲Contents 必須和條件2中#Contents一致)app
<textarea name="Contents" id="Contents" style="width:600px;height:450px;"></textarea>編輯器
三、上傳圖片服務器端代碼(imgFile必須和filePostName一致)ui
@RequestMapping(value = "/upload", method = {RequestMethod.GET, RequestMethod.POST})
public Object upload(HttpServletRequest request,HttpServletResponse response,@RequestParam(value = "imgFile", required = false) MultipartFile file) {this
try {
//設置Response響應的編碼
response.setContentType("text/html; charset=UTF-8");
//獲取一個Response的Write對象
PrintWriter writer = response.getWriter();編碼
String imageUrl = uploadImage(request, "imgFile", "", "");
if (StringUtil.isEmpty(imageUrl)) {url
} else {
imageUrl = FinanceImageUtil.generateAccessUrl(imageUrl.replaceAll("_792x568", ""));
}
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", "https:/"+imageUrl);
writer.println(obj.toJSONString());
//將writer對象中的內容輸出
writer.flush();
//關閉writer對象
writer.close();
} catch (Exception e) { LOGGER.error("上傳圖片出現異常", e); } return null; }