读书人

kindeditor的图片下传 struts2实现

发布时间: 2012-10-30 16:13:35 作者: rapoo

kindeditor的图片上传 struts2实现


kindeditor的图片下传 struts2实现
?
kindeditor的图片下传 struts2实现
?效果:

1、kindeditor下载:http://www.kindsoft.net/down.php

?

2、a:kindeditor安装:解压下载好的文件,把整个文件夹拷贝到工程目录下面,如:/WebRoot/kindeditor

?????

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="kindeditor/kindeditor.js" charset="utf-8" ></script><title>Kindeditor实例</title><script type="text/javascript">KE.show({id:'content', //下面的textarea的IDheight:'300px',resizeMode:0,skinType:'default',autoOnsubmitMode:'true',imageUploadJson:'system/topic-uploadImage.action'   //上传图片的服务器处理地址,3.4版本以后支持});</script></head><body>Kindeditor实例:<textarea id="content" name="topicVO.content" cols="60" rows="25"></textarea><input type="button" id="publish_btn" value="提交"> </body></html>

??

?? 图片上传处理:

?

public class TopicAction{ /** * 图片对象 */private File imgFile;/** * 图片宽度 */private String imgWidth;/** * 图片高度 */private String imgHeight;/** * 图片对齐方式 */private String align;/** * 图片标题 */private String imgTitle;public File getImgFile() {return imgFile;}public void setImgFile(File 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 getAlign() {return align;}public void setAlign(String align) {this.align = align;}public String getImgTitle() {return imgTitle;}public void setImgTitle(String imgTitle) {this.imgTitle = imgTitle;}                /** * 上传图片 * @return */public String uploadImage(){UserVO user = (UserVO) session.getAttribute(Constants.USER_KEY);String imageName = user.getId()+"_topic_"+new Date().getTime()+Math.random()*10000+".jpg";File dirPath =new File(request.getSession().getServletContext().getRealPath("/")+"\\upload\\articleimage\\");//如果文件夹不存在,创建它if(!dirPath.exists()){dirPath.mkdirs();}//文件存放路径String newPath = dirPath+"\\"+imageName;   //上传文件try {copy(imgFile, newPath);} catch (Exception e1) {e1.printStackTrace();}   String id = "content";           String url = "http://" + request.getServerName() + ":"+ request.getServerPort() + request.getContextPath()         +"/upload/articleimage/" + imageName;   String border = "0";   String result = "<script type=\"text/javaScript\">parent.KE.plugin[\"image\"].insert(\""                  + id                   + "\",\""                  + url                   + "\",\""                  + imgTitle                   + "\",\""                  + imgWidth                   + "\",\""                 + imgHeight                 + "\",\""                 + border + "\""                 +");</script>";   PrintWriter out = null;try {out = encodehead(request, response);} catch (IOException e) {e.printStackTrace();}out.write(result);out.close(); return null;}/**       * 拷贝文件       *        * @param upload文件流       * @param newPath新文件路径和名称       * @throws Exception       */  private void copy(File upload, String newPath) throws Exception {           FileOutputStream fos = new FileOutputStream(newPath);           FileInputStream fis = new FileInputStream(upload);           byte[] buffer = new byte[1024];           int len = 0;           while ((len = fis.read(buffer)) > 0) {               fos.write(buffer, 0, len);           }           fos.close();           fis.close();       }  /** * Ajax辅助方法 获取 PrintWriter * @return * @throws IOException  * @throws IOException  * request.setCharacterEncoding("utf-8");response.setContentType("text/html; charset=utf-8"); */private PrintWriter encodehead(HttpServletRequest request,HttpServletResponse response) throws IOException{request.setCharacterEncoding("utf-8");response.setContentType("text/html; charset=utf-8");return response.getWriter();}}

?注意:上面action中的imgFile、imgWidth、imgHeight、align、imgTitle名字不要改,因为在struts2中需要这里跟form表单中的name属性一致,如果你非要改,那么也得把kindeditor中的image.html中的属性改一下,最好不要改源码!

?

?附录:http://www.kindsoft.net/doc.php?cmd=config

?调用KE.showKE.init时可以设置以下参数。

    id
    TEXTAREA输入框的ID,必须设置。
    数据类型:String
    items
    配置编辑器的工具栏,其中"-"表示换行,"|"表示分隔符。
    数据类型:Array
    默认值:
    ['source', '|', 'fullscreen', 'undo', 'redo', 'print', 'cut', 'copy', 'paste','plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright','justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript','superscript', '|', 'selectall', '-','title', 'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold','italic', 'underline', 'strikethrough', 'removeformat', '|', 'image','flash', 'media', 'table', 'hr', 'emoticons', 'link', 'unlink', '|', 'about']? ? ? ? ? 
    width
    编辑器的宽度,可以设置px或%,比TEXTAREA输入框样式表宽度优先度高。
    数据类型:String
    默认值:TEXTAREA输入框的宽度
    注: 3.2版本开始支持。
    height
    编辑器的高度,只能设置px,比TEXTAREA输入框样式表高度优先度高。
    数据类型:String
    默认值:TEXTAREA输入框的高度
    注: 3.2版本开始支持。
    minWidth
    数据类型:Int
    指定编辑器最小宽度,单位为px。
    默认值:200
    minHeight
    数据类型:Int
    指定编辑器最小高度,单位为px。
    默认值:100
    filterMode
    数据类型:Boolean
    true时过滤HTML代码,false时允许输入任何代码。
    默认值:false
    注: 3.4以前版本的filterMode默认值为true。
    htmlTags
    指定要保留的HTML标记和属性。哈希数组的key为HTML标签名,value为HTML属性数组,"."开始的属性表示style属性。
    数据类型:Object
    默认值:
    {? ? ? ? font : ['color', 'size', 'face', '.background-color'],? ? ? ? span : ['style'],? ? ? ? div : ['class', 'align', 'style'],? ? ? ? table: ['class', 'border', 'cellspacing', 'cellpadding', 'width', 'height', 'align', 'style'],? ? ? ? 'td,th': ['class', 'align', 'valign', 'width', 'height', 'colspan', 'rowspan', 'bgcolor', 'style'],? ? ? ? a : ['class', 'href', 'target', 'name', 'style'],? ? ? ? embed : ['src', 'width', 'height', 'type', 'loop', 'autostart', 'quality','style', 'align', 'allowscriptaccess', '/'],? ? ? ? img : ['src', 'width', 'height', 'border', 'alt', 'title', 'align', 'style', '/'],? ? ? ? hr : ['class', '/'],? ? ? ? br : ['/'],? ? ? ? 'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : ['align', 'style'],? ? ? ? 'tbody,tr,strong,b,sub,sup,em,i,u,strike' : []}? ? ? ? ? 
    注:filterMode为true时有效。3.4版本开始属性可设置style,保留所有inline样式。
    resizeMode
    2或1或0,2时可以拖动改变宽度和高度,1时只能改变高度,0时不能拖动。
    数据类型:Int
    默认值:2
    skinType
    风格类型,default或tinymce。
    数据类型:String
    默认值:default
    注:3.4以前版本和3.4.1版本开始默认值为default,3.4版本的默认值为oxygen。 wyswygMode
    可视化模式或代码模式
    数据类型:Boolean
    默认值:true
    cssPath
    指定编辑器iframe document的CSS,用于设置可视化区域的样式。
    数据类型:String或Array
    默认值:空
    注:3.4.1版本开始可指定多个CSS文件。例如:cssPath : ['a.css', 'b.css'] skinsPath
    指定编辑器的skins目录,skins目录存放风格的css文件和gif图片。
    数据类型:String
    默认值:KE.scriptPath + 'skins/'
    pluginsPath
    指定编辑器的plugins目录。
    数据类型:String
    默认值:KE.scriptPath + 'plugins/'
    minChangeSize
    undo/redo文字输入最小变化长度,当输入的文字变化小于这个长度时不会添加到undo堆栈里。
    数据类型:Int
    默认值:5
    loadStyleMode
    true时自动加载编辑器的CSS。
    数据类型:Boolean
    默认值:true
    注: 3.4版本开始支持。
    urlType
    改变站内本地URL,可设置空、relative、absolute、domain。空为不修改URL,relative为相对路径,absolute为绝对路径,domain为带域名的绝对路径。
    数据类型:String
    默认值:空
    注: 3.4版本开始支持,3.4.1版本开始默认值为空。
    newlineTag
    设置回车换行标签,可设置p、br。
    数据类型:String
    默认值:br
    注: 3.4版本开始支持。
    afterCreate
    设置编辑器创建后执行的回调函数。
    数据类型:Function
    默认值:无
    afterDialogCreate
    设置弹出浮动框创建后执行的回调函数。
    数据类型:Function
    默认值:无
    注: 3.4.3版本开始支持。
    allowUpload
    true或false,true时显示上传图片标签。
    数据类型:Boolean
    默认值:true
    注: 3.4版本开始支持。
    allowFileManager
    true或false,true时显示浏览服务器图片功能。
    数据类型:Boolean
    默认值:false
    注: 3.4版本开始支持。
    referMethod
    设置referMethod后上传图片的POST参数里有referMethod。
    数据类型:String
    默认值:空
    注: 3.4版本开始支持。
    dialogAlignType
    设置弹出框(dialog)的对齐类型,可设置page和空,指定page时按当前页面居中,指定空时按编辑器居中。
    数据类型:String
    默认值:page
    注: 3.4.1版本开始支持。
    imageUploadJson
    指定上传图片的服务器端程序。
    数据类型:String
    默认值:../../php/upload_json.php
    注: 3.4.1版本开始支持。
    fileManagerJson
    指定浏览远程图片的服务器端程序。
    数据类型:String
    默认值:../../php/file_manager_json.php
    注: 3.4.1版本开始支持。

?

1 楼 talent210 2010-07-09 目前我也正在做这个
用在了 千山千水旅游网 http://www.isansui.com 上
发现一个BUG,如果WIDTH HEIGHT不设置,那么就是0,根本看不出来的嘛 2 楼 给太阳洒水 2010-07-09 talent210 写道目前我也正在做这个
用在了 千山千水旅游网 http://www.isansui.com 上
发现一个BUG,如果WIDTH HEIGHT不设置,那么就是0,根本看不出来的嘛不设置就是原图片啊,怎么会是0,建议你到官网上下载,还有自己手动到后天控制一下长宽! 3 楼 chenjun296 2010-10-05 其实不需要自己写那insert的方法,直接返回json格式的数据就行,返回类型是html/txt而不是json,否则会报错

读书人网 >软件架构设计

热点推荐