ckeditor图片上传

来源:互联网 发布:数据库not null 编辑:程序博客网 时间:2024/05/18 00:50

1.替换ckeditor

<tr>    <th width="100"> <font color="red">*</font> 内容:</th>     <td>        <script type="text/javascript" src="${contextPath}/lungPages/ckeditor/ckeditor.js"></script>    <textarea name="TextArea1" id="TextArea1"></textarea>        <script type="text/javascript">        CKEDITOR.replace('TextArea1');        </script>    </td></tr>

2 删除图片预览中的代码
在ckeditor/plugins/image/dialogs/image.js文件,搜索“b.config.image_previewText” 删除 b.config.image_previewText||”单引号中的内容全删了
3 修改image.js,搜索“upload”找到’Upload’,hidden:true。改为false;打开被隐藏上传功能
4 在ckeditor\config.js中配置

config.filebrowserImageUploadUrl = "/publish/ckeditorUpload";

5.controller处理

//格式化时间public static String format(Date date, String pattern) {        DateFormat format = new SimpleDateFormat(pattern);        return format.format(date);    }public void ckeditorUpload() throws IOException{        String extName = "";  //扩展名        String fileName = "";  //文件名称        String newFileName = ""; //新文件名        getResponse().setCharacterEncoding("utf-8");        PrintWriter out = getResponse().getWriter();        String CKEditorFuncNum = getPara("CKEditorFuncNum");        System.out.println(CKEditorFuncNum);        try {            String strPath = JFinal.me().getServletContext().getRealPath("/upload/news");            System.out.println(strPath);            UploadFile uploadFile = getFile();            if(uploadFile==null){                renderHtml("<script type=\"text/javascript\">alert(\"请上传图片文件。\");</script>");            }            String uploadFileName = uploadFile.getFileName();            //获取扩展名            if(uploadFileName.lastIndexOf(".")>-1){                extName = uploadFileName.substring(uploadFileName.lastIndexOf(".")+1);                fileName = uploadFileName.substring(0,uploadFileName.lastIndexOf("."));            }            newFileName = fileName+format(new Date(), "yyMMddHHmmss")+"."+extName;            if(!".jpg.gif.png.bmp.JPG.GIF.PNG.BMP".contains(extName)){                FileKit.delete(uploadFile.getFile());                 //renderHtml("<script type=\"text/javascript\">alert(\"上传的文件有误,请重新上传。\");</script>");                out.print("<font color=\"red\"size=\"2\">*文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)</font>");                renderNull();            }else {                File f = uploadFile.getFile();                InputStream is = new FileInputStream(f);                OutputStream os = new FileOutputStream(new File(strPath+File.separator+newFileName));                try {                    byte[] buffer = new byte[1024*1024];                    while(is.read(buffer)>0){                        os.write(buffer);                    }                } catch (Exception e) {                    // TODO: handle exception                    e.printStackTrace();                }finally{                    if(is != null){                        is.close();                    }if(os != null){                        os.close();                }                }                 //删除原文件               FileKit.delete(uploadFile.getFile());               // 返回给ckeditor               renderHtml("<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", '"+getRequest().getContextPath()+"/upload/news/" + newFileName + "', '');</script>");            }        } catch (Exception e) {            // TODO: handle exception             renderHtml("<script type=\"text/javascript\">alert(\"上传的文件有误,请重新上传。\");javascript:history.go(-1);</script>");             e.printStackTrace();                renderHtml("<script type=\"text/javascript\">alert(\"上传文件失败,请联系管理员。\");</script>");        }    }
0 0