富文本添加

js部分

var editor1;
KindEditor.ready(function(K){
editor1 = K.create("textarea[id='content']",{
resizeType : 1,
allowPreviewEmoticons: false,
allowImageUpload:true,//允许上传图片
allowFileManager:true, //允许对上传图片进行管理
uploadJson:'${ctx}//webnewsmanager/uploadimge', //上传图片的java代码,只不过放在jsp中
fileManagerJson:'${ctx}/js/kindeditor-4.1.10/jsp/file_manager_json.jsp',
afterUpload: function(){this.sync();}, //图片上传后,将上传内容同步到textarea中
afterBlur: function(){this.sync();}, ////失去焦点时,将上传内容同步到textarea中
items : [
'fontname','fontsize', '|','forecolor', 'hilitecolor','bold', 'italic','underline',
'removeformat','|', 'justifyleft','justifycenter', 'justifyright','insertorderedlist',
'insertunorderedlist','|', 'emoticons','link','media','|','image']
});
});

html 部分

<textarea id="content" name="content"  style="width:700px; height:300px;"></textarea>

返回赋值

KindEditor.html("#content",row.news_content);

后台图片上传

@RequestMapping(value = "uploadimge")
public String uploadimge(HttpServletRequest request,HttpServletResponse response){
String groupname="";
String newfilepath= "";
String path=WebNewsController.class.getClassLoader().getResource("//").getPath()+"//client.conf";
//String path = request.getSession().getServletContext().getRealPath("/")+"client.conf";
//String newfilepath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/webfile/download/";
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile(iter.next());
String priNames = file.getOriginalFilename();
// 重命名上传后的文件名
String docType = priNames
.substring((priNames.lastIndexOf(".")));
String newfileName = DateUtils.getFileName() + docType;
try {
ClientGlobal.init(path);
ClientGlobal.setG_charset("UTF-8");
ClientGlobal.setG_network_timeout(100000);
ClientGlobal.setG_connect_timeout(100000);
TrackerClient client=new TrackerClient();
TrackerServer conn = client.getConnection();
StorageServer storageServer=null;
StorageClient storageClient = new StorageClient(conn, storageServer);
String [] zips = storageClient.upload_file(file.getBytes(), docType, null);
groupname=zips[0];
newfileName=zips[1];
newfilepath=zips[0]+"/"+zips[1];
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
String imgWidth="150";
String imgHeight="100";
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", Global.getConfig("newuploadfile") + newfilepath);
obj.put("imgWidth", imgWidth);
obj.put("imgHeight", imgHeight);
PrintWriter out = null;

out = encodehead(request, response);
out.println(obj.toJSONString());

out.close();

/*
String url = newfilepath + newfileName;
String border = "0";
String result ="<mce:script type=/'text/javaScript/'>parent.KE.plugin[/'image/'].insert(/'"+url+"/', /'"+imgWidth+"/', /'"+imgHeight+"/', /'"+border+"/'); </mce:script>";
PrintWriter out = null;
out = encodehead(request, response);
out.write(result);
out.close();*/

}
}


return null;
}

 

posted @ 2017-05-09 14:41  无情风中  阅读(278)  评论(0编辑  收藏  举报