大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Java技巧 > CKEditor添加上传图片

CKEditor添加上传图片

关键词:JavaCKEditorCKEditor添加上传图片  阅读(840) 赞(17)

[摘要]本文主要是对CKEditor添加上传图片的讲解,希望对大家学习CKEditor添加上传图片有所帮助。
    1、index.jsp 页面代码
   
    <%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%><%@ page import=“com.ckeditor.CKEditorConfig” %><%@ page import=“java.util.*” %><%@ taglib uri=“http://ckeditor.com” prefix=“ckeditor”%><!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”><link type=“text/css” rel=“stylesheet” href=“ckeditor/_samples/sample.css” /><title>CKEditor Demo</title></head><body><form action=“CKEditor” method=“post”>
   
    <p>
   
    <label for=“editor1”>Editor 1:</label>
   
    <textarea cols=“80” id=“editor1” name=“editor1” rows=“10”></textarea>
   
    </p>
   
    <p>
   
    <label for=“editor2”>Editor 2:</label>
   
    <%
   
    //String value = “My first <strong>CKEditor</strong> Java tag”;
   
    String value = request.getParameter(“editor2”)==null?“”:request.getAttribute(“editor2”)。toString();
   
    Map<String, String> attr = new HashMap<String, String>();
   
    attr.put(“rows”, “8”);
   
    attr.put(“cols”, “50”);
   
    CKEditorConfig settings2 = new CKEditorConfig();
   
    //settings2.addConfigValue(“width”, “500”);
   
    settings2.addConfigValue(“toolbar”, “Basic”);
   
    %>
   
    <ckeditor:editor basePath=“ckeditor/” textareaAttributes=“<%=attr %>” config=“<%=settings2 %>” editor=“editor2” value=“<%= value %>”/>
   
    </p>
   
    <p><input type=“submit” value=“Submit” /></p></form><%
   
    CKEditorConfig settings = new CKEditorConfig();
   
    settings.addConfigValue(“filebrowserImageUploadUrl”, “imageUpload.action?type=Image”);//上传图片
   
    /*
   
    settings.addConfigValue(“filebrowserUploadUrl”, “imageUpload.action?type=File”);// 上传文件
   
    settings.addConfigValue(“filebrowserFlashUploadUrl”, “imageUpload.action?type=Flash”);//上传flash
   
    */%><ckeditor:replace replace=“editor1” basePath=“ckeditor/” config=“<%=settings %>” /></body></html>
   
    2、图片上传action主要代码
   
    public String execute() throws Exception {
   
    log.info(“upload image…”);
   
    if(upload != null) {
   
    ActionContext context = ActionContext.getContext();
   
    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
   
    HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
   
    String fileUrl = null;
   
    String CKEditorFuncNum = request.getParameter(“CKEditorFuncNum”);
   
    String scheme = request.getScheme();
   
    int port = request.getServerPort();
   
    String serverName = request.getServerName();
   
    String contextPath = request.getContextPath();


   
    /*重命名文件名称*/
   
    Calendar cal = Calendar.getInstance();
   
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMddHHmmssSSS”);
   
    String type = uploadFileName.substring(uploadFileName.lastIndexOf(“.”));
   
    String newFileName = sdf.format(cal.getTime()) + type;
   
    String path = request.getSession()。getServletContext()。getRealPath(“/”);
   
    File destFile = new File(path + UPLOAD_PATH + “/” + newFileName);
   
    FileUtils.copyFile(upload, destFile);
   
    log.info(“上传图片成功。”);
   
    fileUrl = UPLOAD_PATH + newFileName;
   
    response.setContentType(“text/html;charset=UTF-8”);
   
    PrintWriter out = response.getWriter();
   
    out = response.getWriter();
   
    fileUrl = scheme + “://” + serverName+“:”+ port + contextPath + fileUrl;
   
    log.info(“fileUrl:” + fileUrl);
   
    out.write(“<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(”
   
    + CKEditorFuncNum  + “,‘” + fileUrl + “’);</script>”);
   
    out.flush();
   
    out.close();
   
    } else {
   
    log.info(“未上传任何文件。”);
   
    }
   
    return SUCCESS;
   
    }



相关评论