1.在官網下載ckeditor。
2.復制到java web項目目錄。
3.配置配置文件並打開圖像上傳功能。
cke editor . editor config = function(config){
//換行模式
config.enterMode = CKEDITOR。ENTER _ BR
//輸入時:shift+Enter是插入的標簽。
config.shiftEnterMode = CKEDITOR。ENTER _ BR//
//圖像處理
config . pastefromwordremovestyles = true;
config . filebrowserimageuploadurl = " ckuploadimage . action?type = image ";
//去掉ckeditor的“保存”按鈕。
config.removePlugins = ' save
};
4.java後臺處理代碼
//上傳圖片
@Action(value = "/ckUploadImage ",results = { @Result(name = "success ",location = "/upload.jsp") })
公共字符串uploadImages()引發異常{
http servlet request request = servletactioncontext . get request();
文件輸出流fos
string webRoot = request . getsession()。getServletContext()。getRealPath(
"");
//獲取圖像後綴名稱
string partRightType = upload filename . substring(upload filename
。lastIndexOf(" . "));
string CKEditorFuncNum = request . getparameter(" CKEditorFuncNum ");
//確定圖片的格式
如果(!image file . check imagetype(partRightType)){
字符串路徑= " ";
String alt_msg = "抱歉!圖片格式選擇不正確,請選擇GIF,jpeg,PNG格式JPG,圖片!”;
pringWriterToPage(" & lt;腳本類型= ' text/JavaScript ' & gt;window . parent . cke editor . tools . call function("
+ CKEditorFuncNum
+ ", '"
+路徑
+ "' , '"
+ alt_msg
+ "');& lt/script & gt;");
}否則{
嘗試{
upload filename = dateutils . getdatenostyle()+"-"
+uuid . randomuuid()+partRightType;
String savePath = webRoot +常量。上傳圖像路徑;
File uploadFilePath =新文件(save path);
if(uploadfilepath . exists()= = false){
uploadfilepath . mkdirs();
System.out.println("路徑不存在,但已成功創建"+save path);
}否則{
System.out.println("路徑存在"+save path);
}
fos = new FileOutputStream(新文件(保存路徑+上傳文件名));
file inputstream fis = new file inputstream(get upload());
byte[] buffer =新字節[1024];
int len = 0;
while((len = fis . read(buffer))& gt;0) {
fos.write(buffer,0,len);
}
fos . close();
fis . close();
} catch(FileNotFoundException for){
System.out.println("上傳文件為0字節");
}
//String path = " http://"+request . get servername()+":"
//+request . getserverport()+request . getcontextpath()
// +常量。上傳圖像路徑+上傳文件名;
string path = request . getcontextpath()
+常量。上傳圖像路徑+上傳文件名;
String alt _ msg =
pringWriterToPage(" & lt;腳本類型= ' text/JavaScript ' & gt;window . parent . cke editor . tools . call function("
+ CKEditorFuncNum
+ ", '"
+路徑
+ "' , '"
+ alt_msg
+ "');& lt/script & gt;");
}
返回null
}
*其實關鍵代碼是這樣的。
pringWriterToPage(" & lt;腳本類型= ' text/JavaScript ' & gt;window . parent . cke editor . tools . call function("
+ CKEditorFuncNum
+ ", '"
+路徑
+ "' , '"
+ alt_msg
+ "');& lt/script & gt;");