又是一个漫漫长夜。

公司的编辑器坏了,用的是百度编辑器,上传图片的网址被框架给拦截了,我们本地怎么测试都没问题,放到服务器就这样了。和老李找了半天,疯了,没原因的。

笔者以前用过jsp+ckeditor,觉得里面上传功能挺好用,于是想出这个法子,把网站的编辑器换掉。

用的是最新的版本的,4点几的。很有wordpress的感觉,不知道是不是一家的。先预览一下:

代码:

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import util.DateUtil; import com.opensymphony.xwork2.ActionSupport; public class UploadFile extends ActionSupport{
private File upload;
private String uploadContentType;
private String uploadFileName; 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 { HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter(); // CKEditor提交的很重要的一个参数
String callback = ServletActionContext.getRequest().getParameter("CKEditorFuncNum"); String expandedName = ""; //文件扩展名
if (uploadContentType.equals("image/pjpeg") || uploadContentType.equals("image/jpeg")) {
//IE6上传jpg图片的headimageContentType是image/pjpeg,而IE9以及火狐上传的jpg图片是image/jpeg
expandedName = ".jpg";
}else if(uploadContentType.equals("image/png") || uploadContentType.equals("image/x-png")){
//IE6上传的png图片的headimageContentType是"image/x-png"
expandedName = ".png";
}else if(uploadContentType.equals("image/gif")){
expandedName = ".gif";
}else if(uploadContentType.equals("image/bmp")){
expandedName = ".bmp";
}else{
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)');");
out.println("</script>");
return null;
} if(upload.length() > 600*1024){
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'文件大小不得大于600k');");
out.println("</script>");
return null;
} InputStream is = new FileInputStream(upload);
String uploadPath = ServletActionContext.getServletContext().getRealPath("img/upload/"+DateUtil.getDirDate());
System.out.println("uploadpath:"+uploadPath);
File dirfile=new File(uploadPath);
if(!dirfile.exists()){
dirfile.mkdirs();
} String fileName = DateUtil.getDate(); //采用时间+UUID的方式随即命名
fileName += expandedName;
System.out.println("filename:"+fileName);
File toFile = new File(uploadPath, 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(); // 返回“图像”选项卡并显示图片
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + "img/upload/"+ DateUtil.getDirDate() +"/"+ fileName + "','')");
out.println("</script>"); return null;
}
}

里面用到一个时间的工具类,用于按照时间创建目录和文件名字。文件名字精确到秒,以免发生名称重复。

其中:

out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + "img/upload/"+ DateUtil.getDirDate() +"/"+ fileName + "','')"); 

这一句是上传图片后,返回给编辑器的一个图片路径地址。

时间工具类:

package com.util;

import java.util.Calendar;

public class DateUtil {
public static String getDate(){
Calendar calendar=Calendar.getInstance();
int y=calendar.get(Calendar.YEAR);
int m=calendar.get(Calendar.MONTH);
int d=calendar.get(Calendar.DATE);
int h=calendar.get(Calendar.HOUR);
int mi=calendar.get(Calendar.MINUTE);
int s=calendar.get(Calendar.SECOND);
StringBuffer sb=new StringBuffer("");
sb.append(y);
sb.append(m+1);
sb.append(d);
sb.append(h);
sb.append(mi);
sb.append(s);
String date=sb.toString();
return date;
} public static String getDirDate(){
Calendar calendar=Calendar.getInstance();
int y=calendar.get(Calendar.YEAR);
int m=calendar.get(Calendar.MONTH);
int d=calendar.get(Calendar.DATE);
StringBuffer sb=new StringBuffer("");
sb.append(y);
sb.append(m+1);
sb.append(d);
String date=sb.toString();
return date;
} }

其中很有意思的是,上面获取的月份,比当前月份要少一个月,不知道这个是怎么理解(有人说老外是从0开始计算月份的,呵呵),于是加1代表当前月份。

然后在struts.xml中,配置一个action即可,不需要result,返回为null的。

 <action name="uploadfile" class="indexAction" method="uploadfile">
</action>

哦,既然有action,当然要有请求他的。这个请求是点击上传时候发生的,在config.js配置:

    //个人的配置
config.filebrowserUploadUrl="/uploadfile.html";

在结束前插入这一句。我这里后面是html,是因为url重写的。默认情况就是上面的action name。

然后就是在各个页面引入编辑器框架。

     <!-- 插入ckeditor  -->
<textarea rows="70" cols="80" name="content"></textarea>

还要引入以下这个js:

 <script type="text/javascript">CKEDITOR.replace('content');</script>
<script type="text/javascript">
window.onload = function()
{
CKEDITOR.replace( 'content' );
};
</script>

最后是在页面头部引入框架的js:

<script type="text/javascript" src="${webroot}/plugins/ckeditor/ckeditor.js"></script>

顺序貌似说反了,大概就是这样。这个真心比百度编辑器好用,整合也简单方便。

struts2+ckeditor配置图片上传的更多相关文章

  1. 使用struts2完成ckeditor和图片上传

    代码地址如下:http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor_ ...

  2. 关于editor网页编辑器ueditor.config.js 配置图片上传

    最近公司项目在做一个门户网站,其中新闻和简介等部分使用到了ueditor编辑器,但是上级明确指示需要图片上传这个功能,这时却发现图片上传功能不能正常使用,上传时一直报错,网上收了好几个处理办法,都说的 ...

  3. Django配置图片上传

    本文首先实现django中上传图片的过程,然后解决富文本编辑器文件上传的问题. 一. 上传图片 1.在 settings.py 中配置MEDIA_URL  和 MEDIA_ROOT 在 D:\blog ...

  4. ckeditor+jsp+spring配置图片上传

    CKEditor用于富文本输入是极好的,它还有一些插件支持扩展功能,其中图片上传就是比较常用到的.本文简单记录我的实现步骤. 1.CKEditor除了提供三种标准版压缩包下载,还可根据自己的需求进行个 ...

  5. drupal中安装CKEditor文本编辑器,并配置图片上传功能

    一.下载: 1.CKEditor模块 2.IMCE模块 二.安装       1.复制: 下载完上面两个模块之后,解压,将解压后整个文件夹,复制粘贴,放到 sites\all\modules下面,个人 ...

  6. CKEditor实现图片上传

    本人用的CKEditor版本为4.3 CKEditor配置和部署参考CKEditor4.x部署和配置. CKEditor编辑器的工具栏中初始的时候应该是这样子的,没有图片上传按钮 并且预览中有一堆火星 ...

  7. ckeditor 实现图片上传以及预览(亲测有效)

    引用ckeditor <script type="text/javascript" src="static/ckeditor/ckeditor.js"&g ...

  8. ckeditor4.7配置图片上传

    ckeditor作为老牌的优秀在线编辑器,一直受到开发者的青睐. 这里我们讲解下 ckeditor最新版本4.7的图片上传配置. https://ckeditor.com/ 官方 进入下载 https ...

  9. UEditor配置图片上传

    最近项目中需要用到一个图文编辑器功能,因为之前的kingeditor功能过于简陋,所以决定换成Ueditor,前端已经配置好了,这个是后台配置 1,确定前台已经配置好了 2,将编辑器的插件包下载下来, ...

随机推荐

  1. Java 正则表达式详细实例解析

    案例1:判断字符串是否是abc package Regex; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * ...

  2. libevent源码学习

    怎么快速学习开源库比如libevent? libevent分析 - sparkliang的专栏 - 博客频道 - CSDN.NET Libevent源码分析 - luotuo44的专栏 - 博客频道 ...

  3. selenium+python在mac环境上的搭建

    前言 mac自带了python2.7的环境,所以在mac上安装selenium环境是非常简单的,输入2个指令就能安装好 需要安装的软件: 1.pip 2.selenium2.53.6 3.Firefo ...

  4. 20165233 Java第二、三章学习总结

    2017-2018-2 <Java程序设计>第二周学习总结 教材学习内容总结 第二.三章 ch2 标识符与关键字 基本数据类型: 逻辑类型:boolean 整数类型:int.byte.sh ...

  5. django2.0实现数据详情页展示的流程

    思路整理 1 先在urls.py中,定义路由获取的格式 url(r'^detail/(\d+)/$', views.blog_detail), 2 然后在views.py,定义数据获取的方法 def ...

  6. C#中StreamWriter与BinaryWriter的区别兼谈编码。

    原文:http://www.cnblogs.com/ybwang/archive/2010/06/12/1757409.html 参考: 1. <C#高级编程>第六版 2.  文件流和数据 ...

  7. 7.25 9figting!

    TEXT 87 Fund management基金管理   A Miller's tale 米勒传奇(陈继龙编译) Dec 7th 2006 From The Economist print edit ...

  8. Controller和View传递数据的几种方式

    使用ViewBag存储数据,如ViewBag.time=2012/7/1,View中可以直接用ViewBag("time")的方式取出数据. 使用ViewData存储数据,存储对象 ...

  9. centos 搭建owncloud私有云

    ownCloud是一个免费开源的软件,用于为分享文件,日历,联系人,书签和个人音频/视频.非常容易安装和管理. 1:搭建环境配置:你的服务应该工作在LAMP上.参考下面链接去安装一键LAMP服务. h ...

  10. LMAX系统架构

    本文转载自:LMAX系统架构 ,(非常感谢作者yfx416分享好文) 很多架构师都面临这么一个问题:如何设计一个高吞吐量,低延时的系统?面对这个问题,各位都有自己的答案.但面对这个问题,大家似乎渐渐形 ...