ckeditor to increase in the jsp image upload function (2)

sponsored links
Before writing the article explains how to manually open the ckeditor in jsp image upload, but after all, write your own upload interface is really ugly. More search information, find that there are better and more simple way.

Before the reference @ 126/blog/static/890531092010226023136 / and
Are trying to open ckeditor default image upload, but failed to successfully uploaded into the background, this may be due to their callback function call error.

Thank the author, his most simple and beautiful way, but also solve the fundamental problem.

ckeditor to increase in the jsp image upload function (2)

ckeditor to increase in the jsp image upload function (2)

ckeditor to increase in the jsp image upload function (2)

Summarized under the upload feature is very simple in fact open, front only the following code:
// This is followed by the parameters are to upload files, pictures, and  flash
                        CKEDITOR.replace( 'context',{filebrowserUploadUrl : '',   
                                filebrowserImageUploadUrl : '',   
                                filebrowserFlashUploadUrl : ''  

Background components can be apache commons io package in to upload fileupload and operation simple to use struts1.2 write back, the bag comes with the lack of attention struts1.2 ways to add an attachment package.

package info.hellolihui.action;

import java.util.Date;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class CKUploadAction extends DispatchAction {

        //  Allows you to upload the file extensions  
        private final String[] exts = new String[]{"gif","png","jpeg","jpg","bmp"}; 
        public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                        throws Exception {
                System.out.println("  Upload  ");
                ServletContext servletContext = request.getSession().getServletContext();
                //   Upload file path set  
                String path = servletContext.getRealPath("/upload") + "/";
                String typeStr = request.getParameter("type");   
                //  If the type is empty, the default file to upload  
        if (typeStr == null) {   
            typeStr = "file";   
        path += typeStr;
        //   Determine whether the folder exists, does not exist, create  ,  At this point the path with the file name has not been  ,  As  Format  
        File dirTest = new File(path);   
        if (!dirTest.exists()) {   
        //  Use Apache Common component  fileupload  Upload files     
        FileItemFactory factory = new DiskFileItemFactory();   
        ServletFileUpload upload = new ServletFileUpload(factory);   
        //   True path of the file name and file     
        String newName = "";   
        String fileUrl = "";   
        String fileName = ""; //  Only the file name  
        try {   
            List items = upload.parseRequest(request);   
            Map fields = new HashMap();   
            Iterator iter = items.iterator();   
            while (iter.hasNext()) {   
                FileItem item = (FileItem);   
                if (item.isFormField())   
                    fields.put(item.getFieldName(), item.getString());   
                    fields.put(item.getFieldName(), item);   
            // CEKditor  In the file domain  name  Value is  upload   
            FileItem uplFile = (FileItem) fields.get("upload");   
            //   Get the file name and do the deal     
            String fileNameLong = uplFile.getName();   
            fileNameLong = fileNameLong.replace('\\', '/');   
            String[] pathParts = fileNameLong.split("/");   
            fileName = pathParts[pathParts.length - 1];   
            //   Get the file extension     
            String ext = getExtension(fileName);   
            //   Set upload file names to the time named  
            fileName = new Date().getTime() + "." + ext;   
            //   Get the file name  (  No extension  )   
            String nameWithoutExt = getNameWithoutExtension(fileName);   
            File pathToSave = new File(path, fileName);   
            fileUrl = path + "/" + fileName;   
            if (extIsAllowed(ext)) {   
                int counter = 1;   
                while (pathToSave.exists()) {   
                    newName = nameWithoutExt + "_" + counter + "." + ext;   
                    fileUrl = path + "/" + newName;   
                    pathToSave = new File(path, newName);   
            } else {   
                System.out.println("  Invalid file type  : " + ext);   
        }catch (Exception ex) {   
        PrintWriter out = response.getWriter();   

        // CKEditorFuncNum  Is displayed when the location of the callback, this parameter must be     
        //  Does not exist  d:\  The address, to  url
                String url = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/upload/" + typeStr + "/" + fileName;
        String callback = request.getParameter("CKEditorFuncNum");   
        out.println("<script type=\"text/javascript\">");   
        out.println("" + callback   
                + ",'" + url + "',''" + ")");   

                return null;


         *   Way to get the file name    
        private static String getNameWithoutExtension(String fileName) {
                return fileName.substring(0, fileName.lastIndexOf("."));

         *   Method for extension    
        private String getExtension(String fileName) {
                return fileName.substring(fileName.lastIndexOf(".") + 1);

         *   To determine whether to allow the method of extension    
        private boolean extIsAllowed(String ext) {
                ext = ext.toLowerCase();
                List extList = Arrays.asList(this.exts);
                return extList.contains(ext)?true:false;

  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of ckeditor to increase in the jsp image upload function (2)

  • hibernate generic generic DAO

    package org.lzpeng.dao; import; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

blog comments powered by Disqus
Recent Entries
Tag Cloud
Random Entries