SpringBoot集成百度UEditor富文本编辑器,并实现自定义文件上传

  1. 官网下载ueditor jsp版 GitHub - fex-team/ueditor: rich text 富文本编辑器
  2. 解压后复制到src/main/resources/static目录下

             

3.表单页面引入 /ueditor/ueditor.config.js ,/ueditor/ueditor.all.js ,/ueditor/lang/zh-cn/zh-cn.js js文件

4.表单页面代码

<!--html-->
<textarea id="ueContainer"></textarea>

<!--javascript-->
<script type="text/javascript">
var ue = UE.getEditor('ueContainer');
UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function(action) {
    if (action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadimage') {
        return '/business/ueditorctl/uploadfile'; //在这里返回我们实际的上传图片地址
    } else {
        return this._bkGetActionUrl.call(this, action);
    }
}
</script>

5.新建Controller

package com.businessfamily.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.businessfamily.util.FileUtil;

/**
 * <p>
 *	 百度ueditor
 * </p> 
 */
@Controller
@RequestMapping("/ueditorctl")
public class UeditorController {
	    
	/**
	 * 加载配置文件,这个方法只是在加载时调用,上传文件接口在表单页面设置为我们自定义的uploadfile方法
	 */
	@RequestMapping(value = "config") 
	public String ueditor() throws Exception {
		return "redirect:/static/ueditor/jsp/config.json";
	}
	
	
	/**
	 * 自定义文件上传接口
	 * @param upfile upfile参数保证与config.json中imageFieldName设置一致
	 * @return 返回数据格式,是ueditor规定的格式
	 * @throws Exception
	 */
	@RequestMapping(value = "uploadfile")
	@ResponseBody
	public Map<String, Object> upload(MultipartFile upfile) 
			throws Exception {
		Map<String, Object> rs = new HashMap<String, Object>();
		String originalFileName = "";
		try {
			if (!upfile.isEmpty()) {
				originalFileName = upfile.getOriginalFilename();
                //调用自定义上传的方法,这里大家改成自己上传的方法
				int fileType = FileUtil.getFileType(upfile.getOriginalFilename());
				String fileurl = FileUtil.saveFileToQiniu(upfile.getInputStream(), fileType, originalFileName, null);
				System.out.println(fileurl);
                //UEDITOR的规则:不为SUCCESS则显示state的内容
				rs.put("state", "SUCCESS");
                //能访问到你现在图片的路径
				rs.put("url", fileurl); 
				rs.put("title", originalFileName);
				rs.put("original", originalFileName);
			}
		} catch (Exception e) {
			e.printStackTrace();
			rs.put("state", e.getMessage()); 
			rs.put("url", "");
			rs.put("title", "");
			rs.put("original", "");
		}
		return rs;
	}

}

6.修改/static/ueditor/ueditor.config.js文件

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值