不多说直接上代码:
首先,定义ImageForm用来接收KindEditor中form中的数据
package com.morefuntek.bean; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; public class ImageForm extends ActionForm { /** * */ private static final long serialVersionUID = 1L; private String id = ""; private String url = ""; private String imgTitle = ""; private FormFile imgFile; private String imgWidth = ""; private String imgHeight = ""; private String filePath = ""; private String align = ""; private String imgBorder = ""; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getImgTitle() { return imgTitle; } public void setImgTitle(String imgTitle) { this.imgTitle = imgTitle; } public FormFile getImgFile() { return imgFile; } public void setImgFile(FormFile imgFile) { this.imgFile = imgFile; } public String getImgWidth() { return imgWidth; } public void setImgWidth(String imgWidth) { this.imgWidth = imgWidth; } public String getImgHeight() { return imgHeight; } public void setImgHeight(String imgHeight) { this.imgHeight = imgHeight; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getAlign() { return align; } public void setAlign(String align) { this.align = align; } public String getImgBorder() { return imgBorder; } public void setImgBorder(String imgBorder) { this.imgBorder = imgBorder; } }
其次:定义action处理接收的表单数据,并返回相应的提示信息给前台页面!
package com.morefuntek.actions; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; import com.morefuntek.bean.ImageForm; import com.morefuntek.listener.ApplicationListener; public class ImageAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ImageForm imageForm = (ImageForm) form; FormFile imagebean = imageForm.getImgFile(); String imageName = imagebean.getFileName(); System.out.println("上传文件:" + imageName); String path = ApplicationListener.image_file_path + "/" + imageName; InputStream in = null; FileOutputStream out = null; byte[] bytes = new byte[1024]; try { in = imagebean.getInputStream(); out = new FileOutputStream(path); int bytesnum = 0; while ((bytesnum = in.read(bytes)) != -1) { out.write(bytes, 0, bytesnum); out.flush(); } } catch (Exception err) { } finally { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } String url = "imageResource/" + imageName; String temp = "parent.KE.plugin[\"image\"].insert(\'" + imageForm.getId() + "','" + url + "','" + imageForm.getImgTitle() + "','" + imageForm.getImgWidth() + "','" + imageForm.getImgHeight() + "','" + imageForm.getImgBorder() + "');"; StringBuffer sb = new StringBuffer(); sb.append("<html>"); sb.append("<head>"); sb.append("<title>Insert Image</title>"); sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"); sb.append("</head>"); sb.append("<body>"); sb.append("<script type=\"text/javascript\">" + temp + ";</script>"); sb.append("</body>"); sb.append("</html>"); try {// 输出ajax内容 response.setHeader("Content-Type", "text/html;charset=UTF-8"); response.getWriter().write(sb.toString()); } catch (IOException e) { e.printStackTrace(); } return null; } }
jsp页面代码:
<script type="text/javascript" charset="utf-8" src="<%=path%>/editor/kindeditor-min.js"></script> <script type="text/javascript"> KE.show({ id : 'content1', width : '100%', resizeMode : 1, imageUploadJson : '<%=path%>/newImage.do', allowFileManager : true, afterCreate : function(id) { KE.event.ctrl(document, 13, function() { KE.util.setData(id); document.forms['example'].submit(); }); KE.event.ctrl(KE.g[id].iframeDoc, 13, function() { KE.util.setData(id); document.forms['example'].submit(); }); } }); function sub() { document.forms[0].submit(); } </script>
全部搞定ok!