ckeditor java 上传_java使用CKEditor实现图片上传功能
java如何使用ckeditor實(shí)現(xiàn)圖片上傳功能,具體內(nèi)容如下
1.根據(jù)實(shí)際需要下載指定的ckeditor
2.刪除文件ckeditor/plugins/image/dialogs/image.js預(yù)覽框中文本內(nèi)容,并修改hidden屬性值為顯示上傳選項(xiàng)卡
刪除image.js中包含在雙引號(hào)中的上述文本
將image.js中的hidden屬性值改為0
3.修改ckeditor/config.js文件,配置“上傳到服務(wù)器”按鈕調(diào)用的controller接口
4.“上傳到服務(wù)器”按鈕調(diào)用的controller級(jí)別的接口
@controller
@requestmapping("publicutil")
public class publicutilcontroller {
@requestmapping(value = "uploadimage")
private void uploadimage(httpservletrequest request, httpservletresponse response, httpsession session,@requestparam multipartfile[] upload) {
response.setcharacterencoding("utf-8");
printwriter out=null;
try {
out = response.getwriter();
} catch (ioexception e1) {
logger.error("response.getwriter()異常="+e1);
e1.printstacktrace();
}
string callback = request.getparameter("ckeditorfuncnum");
// 獲得response,request
map m = new hashmap();
if (!servletfileupload.ismultipartcontent(request)) {
m.put("error", 1);
m.put("message", "請(qǐng)選擇文件!");
//return m;
logger.info("請(qǐng)選擇文件!");
}
string originalfilename=null;//上傳的圖片文件名
string fileextensionname=null;//上傳圖片的文件擴(kuò)展名
for (multipartfile file : upload) {
if (file.getsize()> 10*1024* 1024) {
out.println("
out.println("window.parent.ckeditor.tools.callfunction(" + callback
+ ",''," + "'文件大小不得大于10m');");
out.println("");
}
originalfilename=file.getoriginalfilename();
logger.info("上傳的圖片文件名="+originalfilename);
fileextensionname= originalfilename.substring(
originalfilename.lastindexof(".") ,originalfilename.length()).tolowercase();
logger.info("圖片文件擴(kuò)展名="+fileextensionname);
string[] imageextensionnamearray= websiteconstant.image_extension_name_array;
string allimageextensionname="";
boolean iscontain=false;//默認(rèn)不包含上傳圖片文件擴(kuò)展名
for(int i=0;i
if(fileextensionname.equals(imageextensionnamearray[i])){
iscontain=true;
}
if(i==0){
allimageextensionname+=imageextensionnamearray[i];
}else{
allimageextensionname+=" , "+imageextensionnamearray[i];
}
}
string newfilename=java.util.uuid.randomuuid().tostring()+fileextensionname;
string uploadpath =websiteconstant.pic_app_file_system_ckeditor_location;
if(iscontain){//包含
file pathfile = new file(uploadpath);
if (!pathfile.exists()) { // 如果路徑不存在,創(chuàng)建
pathfile.mkdirs();
}
try {
fileutils.copyinputstreamtofile(file.getinputstream(), new file(uploadpath ,newfilename));
// inputstream is=file.getinputstream();
// file tofile = new file(uploadpath, newfilename);
// 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();
} catch (ioexception e) {
logger.error("fileutils.copyinputstreamtofile uploadpath="+uploadpath+" newfilename ="+newfilename+" exception="+e);
}
string imageurl=websiteconstant.pic_app_server_url+"images/ckeditor/"+newfilename;
// 返回"圖像信息"選項(xiàng)卡并顯示圖片 ,在對(duì)應(yīng)的文本框中顯示圖片資源url
out.println("
out.println("window.parent.ckeditor.tools.callfunction(" + callback
+ ",'" +imageurl + "','')");
out.println("");
}else{
out.println("
out.println("window.parent.ckeditor.tools.callfunction(" + callback
+ ",''," + "'文件格式不正確(必須為"+allimageextensionname+"文件)');");
out.println("");
}
}
}
}
public class websiteconstant {
public static string[] image_extension_name_array={".jpg",".jpeg",".png",".gif",".bmp"};
public static string pic_app_server_url="http://localhost:8090/picture/";
public static string pic_app_file_system_ckeditor_location="/users/abc/documents/tomcat/webapps/picture/images/ckeditor/";
public static final int success = 1; // 操作成功
5.若是在maven項(xiàng)目中使用的ckeditor,需要在pom.xml中添加如下代碼:
com.ckeditor
ckeditor-java-core
3.5.3
6.最終效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持萬仟網(wǎng)。
希望與廣大網(wǎng)友互動(dòng)??
點(diǎn)此進(jìn)行留言吧!
總結(jié)
以上是生活随笔為你收集整理的ckeditor java 上传_java使用CKEditor实现图片上传功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android unix时间,andro
- 下一篇: pbl和sbl_谈PBL和SBL教学法结