百度Ueditor富文本编辑器抓取远程图片

action:catchimage   

对应js:ueditor.all.js中的catchremoteimage方法

原理:通过后台传递的新url地址替换原本的url地址

过程:

1、在调用的ueditor的页面js中将远程图片的地址重写:

var ue = UE.getEditor('webview',{initialFrameHeight:300,initialFrameWidth:600 });
UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function(action) {
   if (action == 'uploadimage' || action == 'uploadfile' ) {
       return CommonJs.getBasePath() +'common/ueditorUploadFile.json?floder=yimifin/advertise/';
   }else if(action == 'catchimage'){//重定义远程文件上传地址
    return CommonJs.getBasePath() +'common/ueditorUploadRemoteFile.json?floder=yimifin/advertise/';
   } else {
       return this._bkGetActionUrl.call(this, action);
   }
};
// 复写UEDITOR的getContentLength方法 解决富文本编辑器中一张图片或者一个文件只能算一个字符的问题,可跟数据库字符的长度配合使用
UE.Editor.prototype._bkGetContentLength = UE.Editor.prototype.getContentLength;
UE.Editor.prototype.getContentLength = function(){
   return this.getContent().length;
}

 

2、修改相应的后台代码:注意返回数据的格式

public void ueditorUploadRemoteFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String floder = request.getParameter("floder");
    String[] sources = request.getParameterValues("source[]");
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("list", fileUploadService.uploadRemoteFileToQiNiu(sources, floder));
    jsonObject.put("state", "SUCCESS");
    response.getWriter().print(jsonObject);
}
public List uploadRemoteFileToQiNiu(String[] sources, String uploadRootPath)
throws Exception {
// TODO Auto-generated method stub
// 永远保存新的文件名
        String newFileName = null;
        List result = new ArrayList();
        for(int i = 0 ; i < sources.length ; i++){
        String source = sources[i];
        if(StringUtils.hasLength(source)){
        File dirFile = new File(source);
                // 如果当前路径不存在,那么创建
                if (!dirFile.exists()) {
                dirFile.mkdirs();
                }
                URL url = new URL(source);  
        HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
        httpUrl.connect();
             // 上传文件
                try {
                    // 取出后缀名
                    String hzm = source.substring(source.lastIndexOf("."),source.length());
                    // 文件上传
                    newFileName = UUID.randomUUID() + hzm;
                    InputStream inputStream = httpUrl.getInputStream();
   //上传文件
                    Map map = new HashMap();
                    map.put("source", source);
                    map.put("url", fileShowPrefixPath+fileUploadRootPath+newFileName);
                    map.put("state", "SUCCESS");
                    result.add(map);
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        }
        
        // 如果文件名为null,那么返回空
        return result;
}

 

3、抓取上传如下图: