Content

ckeditor和ueditor那个好 CKEditor和UEditor使用比较(ckeditor编辑器模糊)难以置信

文章摘要

本文介绍了名为`CkeditorUploadAction`的JavaServlet Action类,用于处理文件上传操作。该类的主要功能包括: 1. **上传验证**:在`execute()`方法中,首先验证上传的文件是否存在以及文件类型和格式是否符合要求(如`.jpg`、`.png`、`.gif`、`.bmp`等)。 2. **文件路径处理**:根据配置文件`wxConfig`获取保存路径,并确保该路径为有效目录。 3. **文件命名**:使用系统时间生成唯一文件名,并将文件扩展名与上传文件名结合,生成最终的文件路径。 4. **文件保存**:通过`InputStream`和`FileOutputStream`将上传文件写入目标路径。 5. **调用CKEditor工具**:在成功上传后,通过`callFunction`方法调用CKEditor的相应功能。 该类通过参数获取、路径处理和文件操作,实现了对上传文件的完整生命周期管理。

 //CkeditorUploadAction类源码 public class CkeditorUploadAction extends ActionSupport { private static final long serialVersionUID=1L; private File upload; private String uploadContentType; private String uploadFileName; //文件大小 private long fileSize; //取文件路径 private String fileType; public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType=fileType; } public long getFileSize() { return fileSize; } public void setFileSize(long fileSize) { this.fileSize=fileSize; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload=upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType=uploadContentType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName=uploadFileName; } public String execute() throws Exception { try { HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType(“text/html;charset=UTF-8”); PrintWriter out=response.getWriter(); String callback=ServletActionContext.getRequest().getParameter(“CKEditorFuncNum”); //对文件进行校验 if(upload==null || uploadContentType==null || uploadFileName==null){ //out.print(“<font color=”red” size=”2″>*请选择上传文件</font>”); String path=””; String alt_msg=”*请选择上传文件”; out.print(“<script type=’text/javascript’>window.parent.CKEDITOR.tools.callFunction(” + callback + “, ‘” + path + “‘ , ‘” + alt_msg + “‘);</script>”); return null; } if ((uploadContentType.equals(“image/pjpeg”) || uploadContentType.equals(“image/jpeg”)) && (uploadFileName.substring(uploadFileName.length() – 4).toLowerCase().equals(“.jpg”)||uploadFileName.substring(uploadFileName.length() – 5).toLowerCase().equals(“.jpeg”))) { //IE6上传jpg图片的headimageContentType是image/pjpeg,而IE9以及火狐上传的jpg图片是image/jpeg }else if((uploadContentType.equals(“image/x-png”) || uploadContentType.equals(“image/png”)) && uploadFileName.substring(uploadFileName.length() – 4).toLowerCase().equals(“.png”)){ }else if(uploadContentType.equals(“image/gif”) && uploadFileName.substring(uploadFileName.length() – 4).toLowerCase().equals(“.gif”)){ }else if(uploadContentType.equals(“image/bmp”) && uploadFileName.substring(uploadFileName.length() – 4).toLowerCase().equals(“.bmp”)){ }else{ //out.print(“<script language=”javascript”>alert(“*文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)”);return false;</script>”); String path=””; String alt_msg=”*请选择图片文件格式(必须为.jpg/.jpeg/.gif/.bmp/.png文件)”; out.print(“<script type=’text/javascript’>window.parent.CKEDITOR.tools.callFunction(” + callback + “, ‘” + path + “‘ , ‘” + alt_msg + “‘);</script>”); return null; } if(upload.length() > this.getFileSize()){ //out.print(“<font color=”red” size=”2″>*文件大小不得大于”+this.getFileSize()/(1000*1024)+”m</font>”); String path=””; String alt_msg=”*请选择上传”+this.getFileSize()/(1000*1024)+”M以内的图片文件”; out.print(“<script type=’text/javascript’>window.parent.CKEDITOR.tools.callFunction(” + callback + “, ‘” + path + “‘ , ‘” + alt_msg + “‘);</script>”); return null; } String imagePath=””; //imagePath路径的设置 WebApplicationContext wac=ContextLoader.getCurrentWebApplicationContext(); WxConfig wxConfig=(WxConfig)wac.getBean(“wxConfig”); //if(fileType.equals(PubParaConstants.DC_CK_UPLOAD_PATTH_PARAM.DC_CK_UPLOAD_PATTH_PARAM_PROSELECT)) { imagePath=wxConfig.getFilePath(); //} //WxConfig wxConfig; File directory=new File(imagePath); if(!directory.isDirectory()) { directory.mkdirs(); } //将文件保存到项目目录下 InputStream is=new FileInputStream(upload); Date date=new Date(); // 获得系统时间,用于生成文件名 long lTime=date.getTime(); String fileName=DateUtil.toStringNoInterval(date, 8)+”_”+lTime; fileName +=FileUtil.getFileSuffix(uploadFileName); File toFile=new File(imagePath, fileName); OutputStream os=new FileOutputStream(toFile); byte[] buffer=new byte[1024]; int length=0; while ((length=is.read(buffer)) > 0) { os.write(buffer, 0, length); } is.close(); os.close(); //设置返回“图像”选项卡 String callbackUrl=ServletActionContext.getRequest().getContextPath() +”/fckImageReader.servlet?fold=”+this.getFileType()+”&imageName=”+fileName; out.println(“<script type=”text/javascript”>”); out.println(“window.parent.CKEDITOR.tools.callFunction(” + callback + “,'”+ callbackUrl + “‘,”)”); out.println(“</script>”); } catch (Exception e) { e.printStackTrace(); } return null; } }