KindEditor的上传图片CGI(JSP版) (转)

[size=x-large]见过、用过很多网页在线编辑器,如:fckEditor、eWebEditor、htmlarea等,也简单看过、修改过其中的代码。但我觉得KindEditor的框架结构最为小巧、合理、精致。程序代码比较优美、易懂。

  功能上,有个我也比欣赏的,就可提供了方便的上传本地图片的扩展编程接口,自带了asp、php的上传CGI程序。我做的项目里一般用java用的比较多一点。可官方没有提供,就自已写了一个。相信有跟我一样需求的朋友,把程序贴在下面,大家拿去用好了。

  我还添加了一个上传附件的功能,跟上传本地图片编程方法基本一致,就不过多介绍了。

/****************************upload.jsp代码**********************************/
Html代码
<% @page import = "java.net.URLDecoder" %>
<%@ page language= "java" contentType= "text/html;charset=GB18030" %>
<%@ page import = "java.util.*" %>
<%@ page import = "org.apache.commons.fileupload.*" %>
<%
//程序: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>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<ExtArr.length; m++){
if (ExtArr[m].equalsIgnoreCase(ext)){
b= true ;
break ;
}
}
if (b== false ){
Msg=Msg2;
break ;
}
FilePath = SavePath + FileNameAuto+ext;
FileUrl = SaveUrl + FileNameAuto+ext;
java.io.File f= new java.io.File(FilePath);
item.write(f);
}
else {
String fieldValue = item.getString();
if ( "imgWidth" .equals(fieldName)){
FileWidth = fieldValue;
}
else if ( "imgHeight" .equals(fieldName)){
FileHeight = fieldValue;
}
else if ( "imgBorder" .equals(fieldName)){
FileBorder = fieldValue;
}
else if ( "imgTitle" .equals(fieldName)){
FileTitle = fieldValue;
}
else if ( "imgTitle2" .equals(fieldName)){
//FileTitle2 = URLDecoder.decode(fieldValue,"GB18030");
FileTitle2 = URLDecoder.decode(fieldValue, "UTF-8" );
}
else if ( "imgAlign" .equals(fieldName)){
FileAlign = fieldValue;
}
else if ( "imgHspace" .equals(fieldName)){
FileHspace = fieldValue;
}
else if ( "imgVspace" .equals(fieldName)){
FileVspace = fieldValue;
}
}
}
if (FileUrl!= null ){
out.println( "<html>" );
out.println( "<head>" );
out.println( "<title>error</title>" );
out.println( "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">" );
out.println( "</head>" );
out.println( "<body>" );
out.println( "<script type=\"text/javascript\">parent.KindInsertImage(\"" + FileUrl + "\",\"" + FileWidth + "\",\"" + FileHeight + "\",\"" + FileBorder + "\",\"" + FileTitle2 + "\",\"" + FileAlign + "\",\"" + FileHspace + "\",\"" + FileVspace + "\");</script>" );
out.println( "</body>" );
out.println( "</html>" );
}
else {
out.println( "<html>" );
out.println( "<head>" );
out.println( "<title>error</title>" );
out.println( "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">" );
out.println( "</head>" );
out.println( "<body>" );
out.println( "<script type=\"text/javascript\">alert(\"" + Msg + "\");parent.KindDisableMenu();parent.KindReloadIframe();</script>" );
out.println( "</body>" );
out.println( "</html>" );
}
%>

<% @page import = "java.net.URLDecoder" %>
<%@ page language= "java" contentType= "text/html;charset=GB18030" %>
<%@ page import = "java.util.*" %>
<%@ page import = "org.apache.commons.fileupload.*" %>
<%
//程序: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>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<ExtArr.length; m++){
if (ExtArr[m].equalsIgnoreCase(ext)){
b= true ;
break ;
}
}
if (b== false ){
Msg=Msg2;
break ;
}
FilePath = SavePath + FileNameAuto+ext;
FileUrl = SaveUrl + FileNameAuto+ext;
java.io.File f= new java.io.File(FilePath);
item.write(f);
}
else {
String fieldValue = item.getString();
if ( "imgWidth" .equals(fieldName)){
FileWidth = fieldValue;
}
else if ( "imgHeight" .equals(fieldName)){
FileHeight = fieldValue;
}
else if ( "imgBorder" .equals(fieldName)){
FileBorder = fieldValue;
}
else if ( "imgTitle" .equals(fieldName)){
FileTitle = fieldValue;
}
else if ( "imgTitle2" .equals(fieldName)){
//FileTitle2 = URLDecoder.decode(fieldValue,"GB18030");
FileTitle2 = URLDecoder.decode(fieldValue, "UTF-8" );
}
else if ( "imgAlign" .equals(fieldName)){
FileAlign = fieldValue;
}
else if ( "imgHspace" .equals(fieldName)){
FileHspace = fieldValue;
}
else if ( "imgVspace" .equals(fieldName)){
FileVspace = fieldValue;
}
}
}
if (FileUrl!= null ){
out.println( "<html>" );
out.println( "<head>" );
out.println( "<title>error</title>" );
out.println( "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">" );
out.println( "</head>" );
out.println( "<body>" );
out.println( "<script type=\"text/javascript\">parent.KindInsertImage(\"" + FileUrl + "\",\"" + FileWidth + "\",\"" + FileHeight + "\",\"" + FileBorder + "\",\"" + FileTitle2 + "\",\"" + FileAlign + "\",\"" + FileHspace + "\",\"" + FileVspace + "\");</script>" );
out.println( "</body>" );
out.println( "</html>" );
}
else {
out.println( "<html>" );
out.println( "<head>" );
out.println( "<title>error</title>" );
out.println( "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">" );
out.println( "</head>" );
out.println( "<body>" );
out.println( "<script type=\"text/javascript\">alert(\"" + Msg + "\");parent.KindDisableMenu();parent.KindReloadIframe();</script>" );
out.println( "</body>" );
out.println( "</html>" );
}
%>

注:此代码在2.4KindEditor版本中测试通过。目前的最新版本为:3.1.1,其js代码变化比较大,不知还能不能用,我没有进行测试。[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值