熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

CKEditor添加上傳圖片

2022-06-13   來源: Java核心技術 
   indexjsp 頁面代碼
   
    <%@ page language=java contentType=text/html; charset=UTF pageEncoding=UTF%><%@ page import=comckeditorCKEditorConfig %><%@ page import=javautil* %><%@ taglib uri= prefix=ckeditor%><!DOCTYPE html PUBLIC //WC//DTD HTML Transitional//EN ><html><head><meta httpequiv=ContentType content=text/html; charset=UTF><link type=text/css rel=stylesheet href=ckeditor/_samples/samplecss /><title>CKEditor Demo</title></head><body><form action=CKEditor method=post>
   
    <p>
   
    <label for=editor>Editor :</label>
   
    <textarea cols= id=editor name=editor rows=></textarea>
   
    </p>
   
    <p>
   
    <label for=editor>Editor :</label>
   
    <%
   
    //String value = My first <strong>CKEditor</strong> Java tag;
   
    String value = requestgetParameter(editor)==null?:requestgetAttribute(editortoString()
   
    Map<String String> attr = new HashMap<String String>()
   
    attrput(rows
   
    attrput(cols
   
    CKEditorConfig settings = new CKEditorConfig()
   
    //settingsaddConfigValue(width
   
    settingsaddConfigValue(toolbar Basic
   
    %>
   
    <ckeditor:editor basePath=ckeditor/ textareaAttributes=<%=attr %> config=<%=settings %> editor=editor value=<%= value %>/>
   
    </p>
   
    <p><input type=submit value=Submit /></p></form><%
   
    CKEditorConfig settings = new CKEditorConfig()
   
    settingsaddConfigValue(filebrowserImageUploadUrl imageUploadaction?type=Image//上傳圖片
   
    /*
   
    settingsaddConfigValue(filebrowserUploadUrl imageUploadaction?type=File// 上傳文件
   
    settingsaddConfigValue(filebrowserFlashUploadUrl imageUploadaction?type=Flash//上傳flash
   
    */%><ckeditor:replace replace=editor basePath=ckeditor/ config=<%=settings %> /></body></html>
   
 圖片上傳action主要代碼
   
    public String execute() throws Exception {
   
    (upload image…
   
    if(upload != null) {
   
    ActionContext context = ActionContextgetContext()
   
    HttpServletRequest request = (HttpServletRequest) contextget(ServletActionContextHTTP_REQUEST)
   
    HttpServletResponse response = (HttpServletResponse) contextget(ServletActionContextHTTP_RESPONSE)
   
    String fileUrl = null;
   
    String CKEditorFuncNum = requestgetParameter(CKEditorFuncNum
   
    String scheme = requestgetScheme()
   
    int port = requestgetServerPort()
   
    String serverName = requestgetServerName()
   
    String contextPath = requestgetContextPath()


   
    /*重命名文件名稱*/
   
    Calendar cal = CalendargetInstance()
   
    SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmssSSS
   
    String type = uploadFileNamesubstring(uploadFileNamelastIndexOf())
   
    String newFileName = sdfformat(calgetTime()) + type;
   
    String path = requestgetSession()getServletContext()getRealPath(/
   
    File destFile = new File(path + UPLOAD_PATH + / + newFileName)
   
    pyFile(upload destFile)
   
    (上傳圖片成功
   
    fileUrl = UPLOAD_PATH + newFileName;
   
    responsesetContentType(text/html;charset=UTF
   
    PrintWriter out = responsegetWriter()
   
    out = responsegetWriter()
   
    fileUrl = scheme + :// + serverName+:+ port + contextPath + fileUrl;
   
    (fileUrl: + fileUrl)
   
    outwrite(<script type=text/javascript>windowparentCKEDITORtoolscallFunction(
   
    + CKEditorFuncNum  + + fileUrl + </script>
   
    outflush()
   
    outclose()
   
    } else {
   
    (未上傳任何文件
   
    }
   
    return SUCCESS;
   
    }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26561.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.