config.js 加入如下代码
// Simplify the dialog windows.
config.removeDialogTabs = 'image:advanced;link:advanced';
config.image_previewText=' '; //预览区域显示内容
config.filebrowserUploadUrl="actions/admin_getCkeditorUpload"; //上传方法
var pathNe =am window.document.location.pathname;
//获取带"/"的项目名,如:/uimcardprj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
config.filebrowserImageUploadUrl = projectName+'/admin_getCkeditorUpload.action'; //固定路径
action 方法如下 :
private File upload; //文件
private String uploadContentType; //文件类型
private String uploadFileName; //文件名
/**
* 上传图片
* @return
* @throws Exception
*/
public String getCkeditorUpload() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//对文件进行校验
if(upload==null || uploadContentType==null || uploadFileName==null){
out.print("<font color=\"red\" size=\"2\">*请选择上传文件</font>");
return null;
}
if ((uploadContentType.equals("image/pjpeg") || uploadContentType.equals("image/jpeg"))
&& uploadFileName.substring(uploadFileName.length() - 4).toLowerCase().equals(".jpg")) {
//IE6上传jpg图片的headimageContentType是image/pjpeg,而IE9以及火狐上传的jpg图片是image/jpeg
}else if(uploadContentType.equals("image/png") && uploadFileName.substring(uploadFileName.length() - 4).toLowerCase().equals(".png")){
}else if(uploadContentType.equals("image/gif") && uploadFileName.substring(uploadFileName.length() - 4).toLowerCase().equals(".gif")){
}else if(uploadContentType.equals("image/bmp") && uploadFileName.substring(uploadFileName.length() - 4).toLowerCase().equals(".bmp")){
}else{
out.print("<font color=\"red\" size=\"2\">*文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)</font>");
return null;
}
if(upload.length() > 600*1024){
out.print("<font color=\"red\" size=\"2\">*文件大小不得大于600k</font>");
return null;
}
//将文件保存到项目目录下
InputStream is = new FileInputStream(upload);
String uploadPath = ServletActionContext.getServletContext()
.getRealPath("/img/postImg"); //设置保存目录
String fileName = java.util.UUID.randomUUID().toString(); //采用UUID的方式随机命名
fileName += uploadFileName.substring(uploadFileName.length() - 4);
File toFile = new File(uploadPath, fileName);
OutputStream os = new FileOutputStream(toFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
//设置返回“图像”选项卡
String callback = ServletActionContext.getRequest().getParameter("CKEditorFuncNum");
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + "img/postImg/" + fileName + "','')");
out.println("</script>");
return null;
}