Java-SSM-百度-UEditor-上传图片
2018-02-06 15:59
621 查看
最近,公司项目中需要增加新闻模块。采用了百度的UEditor 来实现对新闻的排版,没有细研究,只是用了他的文本编辑和上传图片的功能。
2.下载:
3.导入到项目中:解压 ueditor1_4_3_2-utf8-jsp (**我项目中该文件改名为ueditor)
1. jsp页面:
1.1 引入js:
1.2 jsp 文本框:
<div id="editor" style="width:794px;height:340px;"></div>
1.3 js:
1.4 修改ueditor/ueditor.config.js 文件:
1.5 修改ueditor/config.json
因为我是直接调用后台的上传文件的接口,所以 imageUrlPrefix 和 imagePathFormat 没有配置
2. 后台接口:
2.1
maven项目中引 jar包:(两种方式)
2.2 controller:(Map为需要返回的参数)
一:文件配置
1.百度UEditor官网:http://ueditor.baidu.com/website/download.html2.下载:
3.导入到项目中:解压 ueditor1_4_3_2-utf8-jsp (**我项目中该文件改名为ueditor)
二:项目代码
简单的来说一下,前端和后台的代码:1. jsp页面:
1.1 引入js:
<!-- 配置文件 --> <script type="text/javascript" src="/ueditor/ueditor.config.js"></script> <!-- 编辑器源码文件 --> <script type="text/javascript" src="/ueditor/ueditor.all.js"></script> <!-- 语言包文件(建议手动加载语言包,避免在ie下,因为加载语言失败导致编辑器加载失败) --> <script type="text/javascript" src="/ueditor/lang/zh-cn/zh-cn.js"></script>
1.2 jsp 文本框:
<div id="editor" style="width:794px;height:340px;"></div>
1.3 js:
$(function() { var ue = UE.getEditor('editor'); UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl; UE.Editor.prototype.getActionUrl = function(action) { if (action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadvideo') { return 'http://localhost:8005/admin/upload/upPicNews'; //调用后台接口上传图片 } else { return this._bkGetActionUrl.call(this, action); } } }); //取文本框中的内容(纯文本内容和脚本内容) var ue = UE.getEditor('editor'); //新闻内容 var contentScript = ue.getContent();//获取html内容,返回: <p>hello</p> if(contentScript != null && contentScript != ""){ contentScript = contentScript.replace(/\"/g,"'");//把脚本中的所有双引号 替换为单引号 } //取文本框中的内容(纯文本内容和脚本内容) var ue = UE.getEditor('editor'); //新闻内容 //获取纯文本内容,返回: hello var content = ue.getContentTxt();
1.4 修改ueditor/ueditor.config.js 文件:
//为编辑器实例添加一个路径,这个不能被注释 UEDITOR_HOME_URL: URL // 服务器统一请求接口路径 , serverUrl: URL + "jsp/controller.jsp"
1.5 修改ueditor/config.json
因为我是直接调用后台的上传文件的接口,所以 imageUrlPrefix 和 imagePathFormat 没有配置
/* 上传图片配置项 */ "imageActionName": "uploadimage", /* 执行上传图片的action名称 */ "imageFieldName": "upfile", /* 提交的图片表单名称 */ "imageMaxSize": 2048000, /* 上传大小限制,单位B */ "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */ "imageCompressEnable": true, /* 是否压缩图片,默认是true */ "imageCompressBorder": 1600, /* 图片压缩最长边限制 */ "imageInsertAlign": "none", /* 插入的图片浮动方式 */ "imageUrlPrefix": "", /* 图片访问路径前缀 */ "imagePathFormat": "", /* 上传保存路径,可以自定义保存路径和文件名格式*/ /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */ /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */ /* {time} 会替换成时间戳 */ /* {yyyy} 会替换成四位年份 */ /* {yy} 会替换成两位年份 */ /* {mm} 会替换成两位月份 * /* {dd} 会替换成两位日期 */ /* {hh} 会替换成两位小时 * /* {ii} 会替换成两位分钟 */ /* {ss} 会替换成两位秒 */ /* 非法字符 \ : * ? " < > | */ /* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */
2. 后台接口:
2.1
maven项目中引 jar包:(两种方式)
<!-- 将ueditor/jsp/lib 移动到 WEB-INF,然后在pom.xml中引入 --> <dependency> <groupId>com.baidu</groupId> <artifactId>ueditor</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${pom.basedir}/src/main/webapp/WEB-INF/lib/ueditor-1.1.2.jar</systemPath> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency>
2.2 controller:(Map为需要返回的参数)
@RequestMapping(value="/upPicNews",method = RequestMethod.POST) @ResponseBody public Map<String,String> uploadImage(@RequestParam(value = "upfile", required = false) CommonsMultipartFile upfile,HttpServletRequest request) throws Exception{ String path = PIC_HOST_REAL_PATH + File.separator + PIC_NEWS_PATH; // 获取本地原图存储路径 File f = new File(path); if (!f.exists()) { f.mkdirs(); } FileItem item = upfile.getFileItem(); //文件路径 String pathFileName = item.getName(); //字节数 long l = item.getSize(); String fileSize = Long.toString(l); //文件名 int start = pathFileName.lastIndexOf("\\"); String fileName = pathFileName.substring(start + 1); //后缀 .jpg int indexName = fileName.lastIndexOf('.'); String subName = fileName.substring(indexName); //新文件名 String nowName = new SimpleDateFormat("yyMMddHHmmss").format(new Date()) +"_"+ fileName; if (upfile.getSize() > 0) { upfile.transferTo(new File(path+ File.separator + nowName));//上传图片 } String strBackUrl = PIC_URL + "news/pic/"; //服务器地址+端口号+项目名称 +详细路径 Map<String,String> map = new HashMap<String,String >(); map.put("title", fileName); //文件原名称 map.put("original", nowName); //文件名称 map.put("size", fileSize); //文件大小(字节数) map.put("state", "SUCCESS"); //是否上传成功 map.put("type", subName); //文件类型 .+后缀名 map.put("url", strBackUrl + nowName); //文件路径 return map; }
相关文章推荐
- 百度UEditor的介绍和图片上传的使用(java)
- Ueditor结合七牛云及百度云存储(JAVA版,ueditor-1.4.3)实现图片文件上传
- 百度富文本编辑器ueditor上传图片存储又拍云java修改
- MVC使用百度开源文本编辑器UEditor实现图文并茂,字数限制,上传图片或涂鸦
- 百度开源富文本编辑器 UEditor配置:图片上传和文件上传独立使用方法
- 解决在ueditor中上传图片通过后台java+SpringMVC添加水印方法
- 百度富文本编辑器UEditor自定义上传图片接口
- 百度ueditor图片上传提示“上传失败,请重试”解决办法
- 百度UEditor图片上传、SpringMVC、Freemarker、Tomcat、Nginx、静态资源
- Ueditor结合七牛云及百度云存储(JAVA版,ueditor-1.4.3)实现图片文件上传
- 百度UEditor 用require 引入 Thinkphp5 ,图片上传问题
- 20150129 百度Ueditor 上传本地图片失败的处理
- [置顶] 百度ueditor编辑器将图片上传到非项目根路径(保存在磁盘上)或独立服务器解决方案
- ASP.NET百度Ueditor编辑器实现上传图片添加水印效果
- asp.net应用百度编辑器(UEditor)上传图片跟上次附件不成功的解决办法
- ueditor上传图片到七牛云存储(form api,java)
- 百度Ueditor富文本编辑器上传图片
- 百度UEditor图片上传、SpringMVC、Freemarker、Tomcat、Nginx、静态资源
- 百度富文本编辑器UEditor1.3上传图片附件等
- 百度Ueditor改变图片上传路径