Uploading pictures using Kindeditor

Source: Internet
Author: User

To the customer to create the project needs to add rich text, from the Internet to see a lot of people recommend Kindeditor this editor, after using it also feel good, there are some problems is to upload pictures when encountered some problems, recorded here, but also convenient to view later.

First in the official website Download Kindeditor compressed package, (I use here is kindedito-4.1.7), unzip Open, the JSP, plugins, skins, kindeditor.js, Kindedditor-min.js put it into your project (I put the new folder underneath the Webroot kindeditor below), the rest can be left out.

Download the compressed package has demo we can refer to, upload_json.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%@ page import=" java.util.*,java.io.* "%><%@ page import=" Java.text.SimpleDateFormat "%><%@ page import=" org.apache.commons.fileupload.* "%><%@ page import=" org.apache.commons.fileupload.disk.* "%><%@ page import=" org.apache.commons.fileupload.servlet.* "%><% @ Page import= "org.json.simple.*"%><%//File save directory path string savepath = Pagecontext.getservletcontext (). Getrealpath ( "/") + "attached/";//File save directory URLString Saveurl = Request.getcontextpath () + "/attached/";//define allowed upload of file extension 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");//maximum file size long maxSize = 1000000; Response.setcontenttype ("text/html; Charset=utf-8 "); if (! Servletfileupload. Ismultipartcontent (Request) {Out.println (GetError ("Please select File.    ")); return;} Check the directory file Uploaddir = new file (Savepath), if (!uploaddir.isdirectory ()) {out.println ("The upload directory does not exist.    ")); return;} Check the directory Write permission if (!uploaddir.canwrite ()) {Out.println (GetError ("Upload directory does not have write permission.    ")); return;} String dirName = Request.getparameter ("dir"), if (dirName = = null) {DirName = "image";} if (!extmap.containskey (DirName)) {out.println (GetError ("directory name is incorrect.    ")); return;} Create folder Savepath + = DirName + "/"; Saveurl + + DirName + "/"; File Savedirfile = new file (Savepath), if (!savedirfile.exists ()) {savedirfile.mkdirs ();} SimpleDateFormat SDF = new SimpleDateFormat ("YyyyMMdd"); String ymd = Sdf.format (New Date ()), Savepath + = Ymd + "/"; Saveurl + = Ymd + "/"; File Dirfile = new file (Savepath), if (!dirfile.exists ()) {dirfile.mkdirs ();} Fileitemfactory factory = new Diskfileitemfactory (); Servletfileupload upload = new Servletfileupload (factory); Upload.setheaderencoding ("UTF-8"); List items = upload.parserequest (request); Iterator ITR = Items.iterator (); while (Itr.hasnext ()) {Fileitem item = (Fileitem) itr.next ();    String fileName = Item.getname ();    Long fileSize = Item.getsize (); if (!item.isformfield ()) {//Check file size if (Item.getsize () > MaxSize) {out.println (GetError ("Upload file size Limit exceeded.            "));        Return        }//Check the extension String fileext = filename.substring (Filename.lastindexof (".") + 1). toLowerCase (); if (! Arrays.<string>aslist (Extmap.get (DirName). Split (",")). Contains (Fileext)) {out.println (GetError ("The upload file name extension is not The allowed extension. \ n only allow "+ extmap.get (dirName) +" format.            "));        Return        } SimpleDateFormat df = new SimpleDateFormat ("Yyyymmddhhmmss");        String NewFileName = Df.format (New Date ()) + "_" + New Random (). Nextint (+) + "." + Fileext;            try{file UploadedFile = new file (Savepath, newfilename);        Item.write (UploadedFile); }catch (Exception e) {out.println (GetError ("UpperFailed to pass the file.            "));        Return        } jsonobject obj = new Jsonobject ();        Obj.put ("error", 0);        Obj.put ("url", Saveurl + NewFileName);    Out.println (Obj.tojsonstring ());    }}%><%!private string GetError (String message) {Jsonobject obj = new Jsonobject ();    Obj.put ("Error", 1);    Obj.put ("message", message); return obj.tojsonstring ();} %>

Add the following code to the page where we use kindeditor, where item is a tab, where you can add options according to your needs.

<script charset= "Utf-8" src= ". /js/kindeditor-4.1.7/kindeditor.js "></script><script charset=" Utf-8 "src=". /js/kindeditor-4.1.7/lang/zh_cn.js "></script>kindeditor.ready (function (K) {window.editor = K.crea  Te (' #editor_id ', {items: [' source ', ' | ', ' undo ', ' Redo ', ' | ', ' preview ', ' print ', ' template ', ' Code ', ' Cut ', ' copy ', ' Paste ', ' plainpaste ', ' wordpaste ', ' | ', ' justifyleft ', ' justifycenter ', ' justifyright ', ' Ju Stifyfull ', ' insertorderedlist ', ' insertunorderedlist ', ' Indent ', ' outdent ', ' subscript ', ' superscript ', ' clearhtml ', ' Quickformat ', ' selectall ', ' | ', ' fullscreen ', '/', ' formatblock ', ' fontname ', ' fontsize ', ' | ', ' ForeColor ', ' Hi Litecolor ', ' bold ', ' italic ', ' underline ', ' strikethrough ', ' lineheight ', ' removeformat ', ' | ', ' image ', ' fla Sh ', ' media ', ' insertfile ', ' table ', ' hr ', ' emoticons ', ' baidumap ', ' pagebreak ', ' anchor ', ' link ', ' unlink ', ' | ', ' About '],afterchange:fuNction () {This.sync ();}        }                ); });


  Then modify the Plugins-->image-->image.js

Put one of the

Uploadjson = K.undef (Self.uploadjson, Self.basepath + ' php/upload_json.php '),

Revision changed to

Uploadjson = K.undef (Self.uploadjson, Self.basepath + ' jsp/upload_json.jsp '),

Finally, don't forget to create a new directory named attached in our Tomcat directory to store our images. The name is attached because the folder named attached is the default store for pictures in upload_json.jsp. This method of oneself is also groping to come, hope to be able to communicate with you.

Uploading pictures using Kindeditor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.