---
title: "KindEditor的上传图片CGI(JSP版) (转)"
author: "thtwin"
date: "2010-03-06T16:42:25Z"
tags: ["ASP.net", "CGI", "EXT", "JSP", "编程"]
categories: ["编程语言"]
slug: "kindeditor-cgi-jsp"
draft: false
lastmod: "2010-03-06T16:42:25Z"
id: 609311
url: "https://thtwin.iteye.com/blog/609311"
---

\[size=x-large]见过、用过很多网页在线编辑器，如：fckEditor、eWebEditor、htmlarea等，也简单看过、修改过其中的代码。但我觉得KindEditor的框架结构最为小巧、合理、精致。程序代码比较优美、易懂。 　　功能上，有个我也比欣赏的，就可提供了方便的上传本地图片的扩展编程接口，自带了asp、php的上传CGI程序。我做的项目里一般用java用的比较多一点。可官方没有提供，就自已写了一个。相信有跟我一样需求的朋友，把程序贴在下面，大家拿去用好了。 　　我还添加了一个上传附件的功能，跟上传本地图片编程方法基本一致，就不过多介绍了。 /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*upload.jsp代码\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ Html代码 &lt;% @page import = "java.net.URLDecoder" %&gt; &lt;%@ page language= "java" contentType= "text/html;charset=GB18030" %&gt; &lt;%@ page import = "java.util.\*" %&gt; &lt;%@ page import = "org.apache.commons.fileupload.\*" %&gt; &lt;% //程序:wallimn //时间:2008-08-27 //电邮:wallimn@sohu.com //博客：http://wallimn.iteye.com、http://blog.csdn.net/wallimn //说明：KindEditor图片上传处理CGI， //注意：此程序需要commons-fileupload.jar，请自行下载放到WEB-INF/lib目录下。 //request.setCharacterEncoding("GB18030"); String contextPath = request.getContextPath()+ "\\\\" ; String SavePath = request.getSession().getServletContext().getRealPath( "\\\\")+" attached/"; String SaveUrl = contextPath+ "/attached/" ; String\[] ExtArr = new String\[]{ ".gif" , ".jpg" , ".png" , ".bmp" }; int MaxSize = 4000000 ; String Msg1 = "上传文件大小超过限制。" ; String Msg2 = "上传文件的扩展名不被允许。" ; String Msg3 = "文件上传失败。" ; String Msg=Msg3; //java.io.File files=new java.io.File("."); //String FileName = (String)request.getAttribute("fileName"); String FileWidth = null ; String FileHeight = null ; String FileBorder = null ; String FileTitle = null ; String FileTitle2 = null ; String FileAlign = null ; String FileHspace = null ; String FileVspace = null ; Date dt = new Date(); Random random = new Random(); random.nextInt(); String FileNameAuto = String.format( "%X\_%X" , new Object\[]{ new Integer(( int )(dt.getTime())), new Integer(random.nextInt())}); String FilePath = null ; String FileUrl = null ; DiskFileUpload fu = new DiskFileUpload(); fu.setSizeMax(MaxSize); // fu.setSizeThreshold( 4096 ); fu.setRepositoryPath( "c:/" ); //ServletRequestContext src = new ServletRequestContext(request); List fileItems = fu.parseRequest(request); Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); if (!item.isFormField()) { String name = item.getName(); long size = item.getSize(); if ((name== null ||name.equals( "" )) && size== 0 ) continue ; if (size&gt;MaxSize) { Msg=Msg1; break ; } //namename = name.replace(':','\_'); //namename = name.replace('\\\\','\_'); int pos = name.lastIndexOf( "." ); String ext = name.substring(pos); boolean b= false ; for ( int m= 0 ;m" ); out.println( "" ); out.println( "error" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); } else { out.println( "" ); out.println( "" ); out.println( "error" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); } %&gt; &lt;% @page import = "java.net.URLDecoder" %&gt; &lt;%@ page language= "java" contentType= "text/html;charset=GB18030" %&gt; &lt;%@ page import = "java.util.\*" %&gt; &lt;%@ page import = "org.apache.commons.fileupload.\*" %&gt; &lt;% //程序:wallimn //时间:2008-08-27 //电邮:wallimn@sohu.com //博客：http://wallimn.iteye.com、http://blog.csdn.net/wallimn //说明：KindEditor图片上传处理CGI， //注意：此程序需要commons-fileupload.jar，请自行下载放到WEB-INF/lib目录下。 //request.setCharacterEncoding("GB18030"); String contextPath = request.getContextPath()+ "\\\\" ; String SavePath = request.getSession().getServletContext().getRealPath( "\\\\")+" attached/"; String SaveUrl = contextPath+ "/attached/" ; String\[] ExtArr = new String\[]{ ".gif" , ".jpg" , ".png" , ".bmp" }; int MaxSize = 4000000 ; String Msg1 = "上传文件大小超过限制。" ; String Msg2 = "上传文件的扩展名不被允许。" ; String Msg3 = "文件上传失败。" ; String Msg=Msg3; //java.io.File files=new java.io.File("."); //String FileName = (String)request.getAttribute("fileName"); String FileWidth = null ; String FileHeight = null ; String FileBorder = null ; String FileTitle = null ; String FileTitle2 = null ; String FileAlign = null ; String FileHspace = null ; String FileVspace = null ; Date dt = new Date(); Random random = new Random(); random.nextInt(); String FileNameAuto = String.format( "%X\_%X" , new Object\[]{ new Integer(( int )(dt.getTime())), new Integer(random.nextInt())}); String FilePath = null ; String FileUrl = null ; DiskFileUpload fu = new DiskFileUpload(); fu.setSizeMax(MaxSize); // fu.setSizeThreshold( 4096 ); fu.setRepositoryPath( "c:/" ); //ServletRequestContext src = new ServletRequestContext(request); List fileItems = fu.parseRequest(request); Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); if (!item.isFormField()) { String name = item.getName(); long size = item.getSize(); if ((name== null ||name.equals( "" )) && size== 0 ) continue ; if (size&gt;MaxSize) { Msg=Msg1; break ; } //name = name.replace(':','\_'); //name = name.replace('\\\\','\_'); int pos = name.lastIndexOf( "." ); String ext = name.substring(pos); boolean b= false ; for ( int m= 0 ;m" ); out.println( "" ); out.println( "error" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); } else { out.println( "" ); out.println( "" ); out.println( "error" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); } %&gt; 注：此代码在2.4KindEditor版本中测试通过。目前的最新版本为：3.1.1，其js代码变化比较大，不知还能不能用，我没有进行测试。\[/size]