富文本ckediter

##<link rel='stylesheet' href='/css/index.css' />

<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>

<textarea name="editor1" id="editor1" rows="10" cols="80">

</textarea>

<div style="margin-top:3rem;">

消息标题:<input type="text" id="ctitle" style="500px;"/>

<input type="button" id="button1" value="提交" style="height:2.5rem;100%;font-size:1.2rem;letter-spacing:3rem;"/>

</div>

<script type="text/javascript">

$(function(){

CKEDITOR.replace( 'editor1', {

filebrowserUploadUrl: '$!{base}/haliluya/uploadimg.html'

});

$("#button1").click(function(){

var editor = CKEDITOR.instances.editor1;

$.ajax({

url : "/haliluya/uploadContent.html",

data : {

'editorData' : editor.getData(),

'ctitle' : $("#ctitle").val()

},

type : "POST",

success : function(result) {

if (result) {

alert("保存成功");

}else{

alert("提交失败");

}

},

error : function(request) {

alert("sorry!");

}

}, "json");

});

});

</script>

package shareAction;

import java.io.File;

import java.io.PrintWriter;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.util.FileCopyUtils;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.MultipartHttpServletRequest;

import shareMode.GoodNews;

import shareService.GoodNewsService;

@Controller

@RequestMapping({"/haliluya"})

public class Halelujah

{

@Autowired

private GoodNewsService goodNewsService;

@RequestMapping({"/uploadContent"})

@ResponseBody

public boolean uploadContent(String editorData, String ctitle)

{

boolean ok = true;

GoodNews goodNews = new GoodNews();

goodNews.setContent(editorData);

goodNews.setCtitle(ctitle);

int insert = this.goodNewsService.insertGoodNewsService(goodNews);

if (insert <= 0) {

ok = false;

}

return ok;

}

@RequestMapping({"/uploadimg"})

public void execute(HttpServletResponse response, MultipartHttpServletRequest multipartHttpServletRequest, HttpServletRequest httpServletRequest)

throws Exception

{

MultipartFile file = multipartHttpServletRequest.getFile("upload");

String filename = file.getOriginalFilename();

String imgtype = filename.substring(filename.lastIndexOf("."));

String localhostUrl = "images/contentImg/";

String ctxPath = multipartHttpServletRequest.getSession().getServletContext().getRealPath("/") + localhostUrl;

File dirPath = new File(ctxPath);

if (!dirPath.exists()) {

dirPath.mkdir();

}

filename = String.valueOf(Math.random()) + imgtype;

File uploadFile = new File(ctxPath + filename);

FileCopyUtils.copy(file.getBytes(), uploadFile);

String URL = httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + ":" + httpServletRequest.getServerPort() + httpServletRequest.getContextPath() + "/";

String callbackUrl = URL + localhostUrl + filename;

String callback = httpServletRequest.getParameter("CKEditorFuncNum");

PrintWriter out = response.getWriter();

out.println("<script type="text/javascript">");

out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + callbackUrl + "','')");

out.println("</script>");

}

@RequestMapping({"/uploadNews"})

public String uploadNews()

{

return "/b/uploadNews";

}

}

原文地址:https://www.cnblogs.com/thankyouGod/p/6022278.html