核心提示:使用CKeditor的图片上传功能editor的图片上使用CK传功能需引入FileUpLoad文件上传jar包:commons-fileupload-1.3.2.jarcommons-io-2.4.j...
使用CKeditor的图片上传功能
editor的图片上使用CK传功能
需引入FileUpLoad文件上传jar包:commons-fileupload-1.3.2.jar
commons-io-2.4.jar
开启图片上传功能:
ckeditor --> config.js
在CKEDITOR.editorConfig = function( config )下加入:
config.filebrowserImageUploadUrl = "imageUpload"; //开启图片上传功能,imageUpload为post请求
以下为Servlet内容(固定用法):
public class ImageUploadServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解决ajax乱码问题 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); FileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload(factory); try { Listlist=upload.parseRequest(request); for (FileItem fileItem : list) { String imageName=DateUtil.getCurrentDateStr(); //将图片保存至硬盘,imagePath=D:\\EclipseNeonWorkSpace\\News\\WebContent\\userImage\\ File file=new File(PropertiesUtil.getValue("imagePath")+imageName+"."+fileItem.getName().split("\\.")[1]); fileItem.write(file); //用于ckEditor回调并显示刚上传的图片,ckEditorCallBackImagePath=userImage String newPath=PropertiesUtil.getValue("ckEditorCallBackImagePath")+"/"+imageName+"."+fileItem.getName().split("\\.")[1]; String callback = request.getParameter("CKEditorFuncNum"); out.println("<script type=\"text/javascript\">"); out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + newPath + "',''" + ")"); out.println("</script>"); out.flush(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }