日期:2014-05-16  浏览次数:20453 次

ckeditor在jsp下增加图片上传功能(2)
     之前写的文章http://fzlihui.iteye.com/admin/blogs/749437说明了如何在jsp中手动开启ckeditor的图片上传,但是毕竟是自己写的上传界面,实在是丑。查了更多资料,发现还有更好更简单的方法。

     之前参考的http://blog.163.com/ytrtfhj@126/blog/static/890531092010226023136/和
http://topic.csdn.net/u/20100127/11/d2254308-db90-4b1f-adca-b36bd8956264.html
都尝试开启ckeditor默认的图片上传,但是都没能成功上传到后台,这可能是由于他们回调函数调用错误。

   感谢http://sarin.iteye.com/blog/599499的作者,他的方法最简单美观,也解决了最根本的问题。






   总结了下其实开启上传功能非常简单,前台只需要以下代码:
//这后面加的参数分别是上传文件,图片和flash
			CKEDITOR.replace( 'context',{filebrowserUploadUrl : 'ckupload.do?type=file',   
				filebrowserImageUploadUrl : 'ckupload.do?type=image',   
				filebrowserFlashUploadUrl : 'ckupload.do?type=flash'  
		    });


后台可以用apache commons组件中的fileupload和io包进行上传操作
用struts1.2简单写了后台,注意struts1.2自带的包里缺少一些方法,要加入附件的包。

package info.hellolihui.action;

import java.io.File;
import java.io.PrintWriter;
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 {

	//允许上传的文件后缀名
	private final String[] exts = new String[]{"gif","png","jpeg","jpg","bmp"}; 
	
	@Override
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("上传");
		
		ServletContext servletContext = request.getSession().getServletContext();
		
		// 设定上传文件路径
		String path = servletContext.getRealPath("/upload") + "/";
		
		String typeStr = request.getParameter("type");   
		//如果type为空则默认为上传文件
        if (typeStr == null) {   
            typeStr = "file";   
        } 
        path += typeStr;
		
        // 判断文件夹是否存在,不存在则创建,此时的path还未加上文件名,为http://127.0.0.1:8080/xx/upload的格式
        File dirTest = new File(path);   
        if (!dirTest.exists()) {   
            dirTest.mkdirs();   
        }   
       
        System.out.println(path);
		
        //使用Apache Common组件中的fileupload进行文件上传   
        FileItemFactory factory = new DiskFileItemFactory();   
        ServletFileUpload upload = new ServletFileUpload(factory);   
        
        // 文件名和文件真实路径   
        String newName = "";   
        String fileUrl = "";   
        String fileName = ""; //只有文件名
        try {   
            List items = upload.parseRequest(request);   
            Map fields = new HashMap();   
            Iterator iter = items.iterator();   
            while (iter.hasNext()) {   
                FileItem item = (FileItem) iter.next();   
                if (item.isFormField())   
                    fields.put(item.getFieldName(), item.getString());   
                else  
                    fields.put(item.getFieldName(), item);   
            }   
            // CEKditor中file域的name值是upload   
            FileItem uplFile = (FileItem) fields.get("upload");   
            // 获取文件名并做处理   
            String fileNameLong = uplFile.getName();   
            fileNameLong = fileNameLo