index.jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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 type="text/javascript" src="<%=path %>/ckeditor/ckeditor.js"></script>
<title>Insert title here</title>
</head>
<body>
<h1> Welcome ${requestScope.name } ${name } ${pageScope.name }</h1>
${sessionScope.name }
<br/>
<form id="form1" name="form1" method="post" action="display.jsp" enctype="multipart/form-data" >
<textarea id="editor1" name="editor1" >zhangzhaoyu</textarea>
<Script type="text/javascript" >
var editor = CKEDITOR.replace( 'editor1',{
filebrowserImageUploadUrl : 'ImageUpload' //上传文件的servlet
});
</Script>
<input type="submit" name="submit" value="提交"/>
<input type="reset" name="reset" value="重置"/>
</form>
</body>
</html>
ckeditor的配置文件:
/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
CKEDITOR.editorConfig = function( config )
{
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
config.language='Zh-CN';
config.uiColor='#CCC';
config.width='900px';
config.height='200px';
config.skin='kama';
config.toolbar='MyToolbar';
config.toolbarCanCollapse=false;
config.resize_enabled=true;
config.toolbarStartupExpanded=true;
config.autoUpdateElement=true;
config.baseHref='';
config.colorButton_backStyle={
element:'span',
styles:{'background':'#CCC'}
};
config.enterMode=CKEDITOR.ENTER_BR;
config.shiftEnterMode=CKEDITOR.ENTER_BR;
config.pasteFromWordRemoveStyles=true;
//config.filebroserImageUploadUrl='ckUploadImage.action?type=image';
//config.removePlugins='true';
//config.editBlock=true;
config.toolbar_MyToolbar =
[
['Source','-','Save','Preview','Print'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','Scayt'],
['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
'/',
['Styles','Format'],
['Bold','Italic','Strike'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['Link','Unlink','Anchor'],
['Maximize','-','About']
];
};
servlet:ImageUpload
package com.zzy.test;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class ImageUpload
*/
public class ImageUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ImageUpload() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload sfu=new ServletFileUpload(factory);
//factory.setRepository(new File(request.getRealPath("/")+"zzy"));
PrintWriter out=response.getWriter();
FileItem item=null;
try {
List<FileItem> list=sfu.parseRequest(request);
Iterator i=list.iterator();
while(i.hasNext()){
item=(FileItem)i.next();
if(!item.isFormField()){
String name=item.getName();
int index=name.lastIndexOf(".");
String attndName=name.substring(index);//文件类型
System.out.println(attndName);
Long t=System.currentTimeMillis();
String nn=request.getRealPath("/")+"\\zzy\\"+t+attndName;
File file=new File(nn);
item.write(file);
String callback = request.getParameter("CKEditorFuncNum");
response.getWriter().println("<script type=\"text/javascript\">");
response.getWriter().println("window.parent.CKEDITOR.tools.callFunction(" + callback
+ ",'" + nn + "',''" + ")");
response.getWriter().println("</script>");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
注明:ckeditor的内容实际上是一个textarea,可以用如下方式获取内容
request.setCharacterEncoding("utf-8");
out.println(request.getParameter("editor1"));