0、了解CKeditor的相关文件以及引入js javascript
引入ckeditor的js php
<script type="text/javascript" src="<c:url value="/resources/js/ckeditor/ckeditor.js" />"></script>
一、开启CKeditor上传图片功能 html
官方说明 java
简明的说,就是只须要在建立CKeditor时,添加如下两个配置: web
在用CKEDITOR.replace()中配置 spring
filebrowserBrowseUrl: '自定义的请求', filebrowserUploadUrl: '自定义的请求'
CKEDITOR.replace('editor',{ filebrowserBrowseUrl: '/browser/browse.php', filebrowserUploadUrl: '/uploader/upload.php' });
或,在config.js添加以下配置 chrome
config.filebrowserBrowseUrl='自定义的请求', config.filebrowserUploadUrl='自定义的请求'
二、编写自定义的请求 跨域
@RequestMapping(value="/ckeditor/uploadFile") public void uploadFile(@RequestParam("upload") MultipartFile multipartFile,HttpServletRequest request,HttpServletResponse response){ response.setContentType("text/html;charset=UTF-8"); // response.setHeader("X-Frame-Options", "SAMEORIGIN"); String CKEditorFuncNum = request.getParameter("CKEditorFuncNum"); PrintWriter out; String s = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction("+CKEditorFuncNum+", '"+multipartFile.getOriginalFilename()+"');</script>"; try { out = response.getWriter(); out.print(s); out.flush(); } catch (IOException e) { e.printStackTrace(); } }
问题及解决方案: app
一、解决ckeditor上传,返回时显示js代码不显示图片的问题 ide
问题解决:
未配置response的ContentType
response.setContentType("text/html;charset=UTF-8");
解决ckeditor上传,返回时显示js代码不显示图片的问题
二、跨域问题
chrome console log:
Refused to display 'http://localhost:8080/webcnmain/ifacompany/article/uploadArticleImageForCkeditor?CKEditor=articleContentZH&CKEditorFuncNum=1&langCode=zh-cn' in a frame because it set 'X-Frame-Options' to 'DENY'.
解决方法
response.setHeader("X-Frame-Options", "SAMEORIGIN");
3.spring 上传文件未配置相关的视图解析器
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?