ueditor:使用笔记:

ueditor:使用笔记:【版本:ueditor1_4_3_3】
1、下载官方源码包,下载源码包(便于在不同环境中使用)
2、导入源码包lib中的所有jar(不要使用ueditor.jar,请使用ueditor源码)

3、将源码jsp案例中的config.json文件拷贝到自定义路径下【此处,我放在WebContent/resource/ueditor1_4_3_3路径下】,删除源码包jsp

    找到ueditor的java源码中ConfigManager.java源码
    按下面方式修改:
private String getConfigPath () {
//return this.parentPath + File.separator + ConfigManager.configFileName;
return this.rootPath
           + File.separator + "resource/ueditor1_4_3_3"
           + File.separator + ConfigManager.configFileName;
}
解析:这个方法用于获取config.json文件所在路径
【查看源码:会发现config.json文件用于存储一些json数据,分别对应各种类型文件参数
作用分析:
java源码断点调试分析:
发现:初始化ueditor过程中会向后台发送一个请求,该请求返回config.json的所有数据
  前端采用getActionUrl(action)方法判断 这里 对应采用的action
  通过json数据控制上传文件参数

4、创建页面文件:
见ueidtor.html
重点是:
UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function(action){alert(action);
if(action == 'uploadimage'){//图片上传的action
return URL+'ueditor/uploadImage';
}else{
return this._bkGetActionUrl.call(this, action);
}
}
重写这段代码,请查看源码ueditor.all.js文件对应方法
此方法会在操作上传文件时响应

5、后台代码编写:【此处我采用的是spring mvc】
1、编写ueditor初始化过程解析config.json的接口:
直接复制jsp里面的内容:


2、一般来说,大多数情况下文件上传不会存储到项目路径下,
    我用的时nginx静态资源访问服务器,
     所以需要重写文件上传接口
 
【接口:】
@Controller
@RequestMapping("/ueidtor")
public class UeidtorController {
/**
*解析config.json
*/
@RequestMapping("/readConfig")
public void readConfig(HttpServletRequest request,HttpServletResponse response) throws IOException {
try {
request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html");
String rootPath = request.getServletContext().getRealPath( "/" );
PrintWriter out=response.getWriter();
out.write( new ActionEnter( request, rootPath ).exec());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
*图片上传
*/
@ResponseBody
@RequestMapping(value="/uploadImage")
public Map<String, Object> uploadImage (MultipartFile upfile, HttpServletRequest request, HttpServletResponse response){
Map<String, Object> params = new HashMap<String, Object>();
try{
String basePath = LoadPropertiesDataUtils.getValue("fileUploadPath");
String visitUrl = LoadPropertiesDataUtils.getValue("fileDownPath");
String ext = StringUtil.getFileExt(upfile.getOriginalFilename());
String fileName = String.valueOf(System.currentTimeMillis()).concat("_").concat(RandomUtils.getRandom(6)).concat(".").concat(ext);
StringBuilder sb = new StringBuilder();
//拼接保存路径
sb.append(basePath).append("/").append(fileName);
visitUrl = visitUrl.concat(fileName);
File f = new File(sb.toString());
if(!f.exists()){
f.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(f);
FileCopyUtils.copy(upfile.getInputStream(), out);
params.put("state", "SUCCESS");
params.put("url", visitUrl);
params.put("size", upfile.getSize());
params.put("original", fileName);
params.put("type", upfile.getContentType());
} catch (Exception e){
params.put("state", "ERROR");
}
return params;
}


6、最后处理文件上传反应迟钝的问题
修改 ueditor.all.js / ueditor.all.min.js / image.js
accept: {
               title: 'Images',
               extensions: acceptExtensions,
              // mimeTypes: 'image/*'
               mimeTypes: 'image/jpg,image/jpeg,image/png'
   },

7、到此,基本功能完毕



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值