读书人

kindeditor4.1.4 下传功能

发布时间: 2012-12-17 09:31:40 作者: rapoo

kindeditor4.1.4 上传功能
最近在做一个项目,要用到kindeditor4.1.4的上传功能 ,但之前又没有用过,本人又是新手,所以搞得我焦头烂额,到处去提问,最后在我们论坛有人推荐我看一篇博客,最后终于解决了我的问题,那篇博客只是图片上传,我又加了一些进去,因本人是新手,所以写博客加深一下印象……废话不多说

说明一下,我用的是SSH2框架

在我没有在论坛提问之前,我就知道了是因为struts2已经把file上传封装了,所以kindeditor官方给的demo获取不到request , 所以我的想法是利用struts2的request来获取kindeditor上传控件的值,但我不知道它的控件叫什么(本人菜鸟,一些常识不懂很正常)后来它的源码被我翻了一整天, 最后在那篇博客找到了……接下来我都懂了,很简单。在这里提醒一下需要用到的人,kindeditor的控件都在它的plugins下面,如图片上传的控件在:plugins/image下

下面是具体的代码:

action里的:

private File imgFile;/** * 文件名称 */private String imgFileFileName;/** * 图片宽度 */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 getImgFileFileName() {return imgFileFileName;}public void setImgFileFileName(String imgFileFileName) {this.imgFileFileName = imgFileFileName;}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;}public String uploadImg() {ServletActionContext.getResponse().setContentType("text/html; charset=UTF-8");// 文件保存目录路径String savePath = ServletActionContext.getServletContext().getRealPath("/")+ "attached/";// 文件保存目录URLString saveUrl = ServletActionContext.getRequest().getContextPath()+ "/attached/";// 定义允许上传的文件扩展名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");// 最大文件大小long maxSize = 2 * 1024 * 1024 * 100;PrintWriter out = null;try {out = ServletActionContext.getResponse().getWriter();} catch (IOException e1) {}if (imgFile == null) {out.println(getError("请选择文件。"));return null;}// 检查目录File uploadDir = new File(savePath);if (!uploadDir.isDirectory()) {out.println(getError("上传目录不存在。"));return null;}// 检查目录写权限if (!uploadDir.canWrite()) {out.println(getError("上传目录没有写权限。"));return null;}String dirName = ServletActionContext.getRequest().getParameter("dir");if (dirName == null) {dirName = "image";}if (!extMap.containsKey(dirName)) {System.out.println(getError("目录名不正确。"));return null;}// 创建文件夹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();}String fileExt = imgFileFileName.substring(imgFileFileName.lastIndexOf(".") + 1).toLowerCase();if (!Arrays.<String> asList(extMap.get(dirName).split(",")).contains(fileExt)) {System.out.println(getError("上传文件扩展名是不允许的扩展名。\n只允许"+ extMap.get(dirName) + "格式。"));return null;}if (imgFile.length() > maxSize) {out.println(getError("[ " + imgFileFileName + " ]超过单个文件大小限制,文件大小[ "+ imgFile.length() + " ],限制为[ " + maxSize + " ] "));return null;}SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");String newFileName = df.format(new Date()) + "_"+ new Random().nextInt(1000) + "." + fileExt;File uploadedFile = new File(savePath, newFileName);try {FileUtil.copyFile(imgFile, uploadedFile);JSONObject obj = new JSONObject();obj.put("error", 0);obj.put("url", saveUrl + newFileName);out.println(obj.toString());} catch (IOException e) {}return null;}private String getError(String message) {JSONObject obj = new JSONObject();obj.put("error", 1);obj.put("message", message);return obj.toString();}



JSP页面:

<link rel="stylesheet" href="kindeditor/themes/default/default.css" />  <link rel="stylesheet" href="kindeditor/plugins/code/prettify.css" />  <script charset="utf-8" src="kindeditor/kindeditor.js"></script>  <script charset="utf-8" src="kindeditor/lang/zh_CN.js"></script>  <script charset="utf-8" src="kindeditor/plugins/code/prettify.js"></script>  <script>KindEditor.ready(function(K) {   var editor1 = K.create('textarea[id="ke_demo"]', {//文本控件idcssPath : 'kindeditor/plugins/code/prettify.css',uploadJson : 'KeAction.action',//提交地址,action要自己配置fileManagerJson : 'kindeditor/jsp/file_manager_json.jsp',allowFileManager : true,afterCreate : function() {var self = this;K.ctrl(document, 13, function() {self.sync();document.forms['example'].submit();});K.ctrl(self.edit.doc, 13, function() {self.sync();document.forms['example'].submit();});}});prettyPrint();                 });</script>//需要用到的地方<textarea id="ke_demo" name="" rows="20" cols="100" style="width:600;height:200px;visibility:hidden;"></textarea>


配置struts.xml:

<action name="KeAction" method="uploadImg"></action>


最后还要配置一下资源文件struts.properties:

struts.multipart.parser=jakartastruts.multipart.maxSize=-1


最后成功了……

这些是我整理出来的,希望能帮到各位

读书人网 >开源软件

热点推荐