用ssh,加插件ckeditor,完成文件图片上传

前台页面,要调用ckeditor的插件跳转到后台。

<script type="text/javascript">
        var editor;
    window.onload = function(){
        editor = CKEDITOR.replace('content',
             {
         toolbar : 'Diy',
         filebrowserImageUploadUrl: "${pageContext.request.contextPath}/news/newsFile"
          }    ); 
    }

</script>

保留插件的方式不同使用方式有所不同,不过大致都是要跳转到后台。

后台是用SpringMVC做的

	@RequestMapping(value="/newsFile", method= RequestMethod.POST)
	public String newFile(HttpServletResponse response, HttpServletRequest request,MultipartFile upload) throws Exception{
	        String realpath=request.getSession().getServletContext().getRealPath("images/upload");
            String uploadContentName = upload.getOriginalFilename();
            String newFileName = StringUtil.getRandomString(); 
	        File  file=new File(realpath + "/" + uploadContentName);
	        response.setCharacterEncoding("UTF-8");  
	        PrintWriter out = response.getWriter();  
	        // CKEditor提交的很重要的一个参数  
	        String callback = request.getParameter("CKEditorFuncNum");   
	        String fileName = file.getName();
	        String prefix = fileName.substring(fileName.lastIndexOf(".")+1);
	        
	        String expandedName = FileUploadUtils.getImagePrefix(prefix); //文件扩展名  

            //这里是为了能反映图片到界面
	        if (expandedName == null) {
	            out.println("<script type=\"text/javascript\">");    
	            out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'" + springMssageSource.getMessage("common.fileUpload.prefixIncorrect", null, LocaleContextHolder.getLocale()) + "');");   
	            out.println("</script>");  
	            return null;  
	        }  
	        
            //这里是为了能反映图片到界面+1
	        long maxImageLength = NumberUtil.toLong(ContextConfigUtil.getInstance().getProperty("UPLOAD_IMAGE_MAX_LENGTH"));
	        if(file.length() > maxImageLength){  
	            out.println("<script type=\"text/javascript\">");    
	            out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'" + springMssageSource.getMessage("common.fileUpload.maxFileSize", null, LocaleContextHolder.getLocale()) + "');");   
	            out.println("</script>");  
	            return null;  
	        }  
	        String sysFileUrl = ContextConfigUtil.getInstance().getProperty("SYS_UPLOAD_IMAGE_URL");
	        String localUrl=ContextConfigUtil.getInstance().getProperty("NEWS_LOGO_FOLDER");
	        //这是写在一起的url,address + newFileName + expandedName,为本地路径,可为服务器地址
            String address=sysFileUrl+localUrl;
	        File localImage=new File(address + newFileName + expandedName);
	        
            //文件上传最重要的方法。
	        FileUtils.copyInputStreamToFile(upload.getInputStream(), localImage);
	        
	        // 返回“图像”选项卡并显示图片  
            out.println("<script type=\"text/javascript\">"); 
            out.println("parent.document.getElementById('cke_63_textInput').value = '" + request.getContextPath() + "/loadImage?imageName=" +"news/"+ newFileName + expandedName + "'");
            out.println("console.log(parent.document.getElementById('cke_57_previewImage').style.width)");
            out.println("parent.document.getElementById('cke_57_previewImage').src = '" + request.getContextPath() + "/loadImage?imageName=" + "news/"+ newFileName + expandedName + "'");
            out.println("parent.document.getElementById('cke_57_previewImage').style.display = 'inline-block'");
            out.println("</script>");  
	        return null;  
	    }  

144857_cYYb_2525325.png

144945_G8yn_2525325.png

144957_Q6SW_2525325.png

145031_4ORl_2525325.png

转载于:https://my.oschina.net/u/2525325/blog/680476

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值