- 官网下载ueditor jsp版 GitHub - fex-team/ueditor: rich text 富文本编辑器
- 解压后复制到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文件