百度富文本编辑器 UEditor 1.4.3 自定义图片保存路径

时间:2022-08-28 18:06:42

百度UEditor图片文件改变默认保存到项目根路径,自定义上传路径或远程服务器:http://blog.csdn.net/slyn_2004/article/details/53868547

1. js实例化编辑器:

//实例化编辑器   
   var ue = UE.getEditor('notice-content');  
   UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;  
   UE.Editor.prototype.getActionUrl = function(action) {  
       if (action == 'uploadimage' || action == 'uploadscrawl' || action == 'listimage') {  
           return '/myproject/ueditorUpload';  
       } else {  
           return this._bkGetActionUrl.call(this, action);  
       }  
   }  

2. 后台(用的是Spring MVC):

 @RequestMapping(value = "/ueditorUpload")    
  public void uploadUEditorImage(@RequestParam(value = "upfile", required = false) MultipartFile file,  
        HttpServletResponse response, HttpServletRequest request) throws Exception {  
            request.setCharacterEncoding("utf-8");  
response.setCharacterEncoding("utf-8");  
JSONObject json=new JSONObject();  
PrintWriter out = response.getWriter();  
  
try {  
    String root = "E:\\tmp";  
            String fileName = file.getOriginalFilename();  
            String fileSuffix = fileName.substring(fileName.indexOf(".")+1);  
  
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");  
            String directory = "\\file\\image\\" + dateFormat.format(new Date()) + "\\";  
            //Util.getRandomString(int length):返回由length个任意字母组成的字符串  
            String path =directory + Util.getRandomString(12) + "." + fileSuffix;  
            new File(root + directory).mkdir();  
    file.transferTo(new File(root + path));  
      
    json.put("state", "SUCCESS");  
    json.put("title", file.getName());  
    json.put("url", path);//图片访问路径  
    json.put("original", file.getName());  
} catch (Exception e) {  
    json.put("state", "上传图片出错");  
}  
out.print(json.toString());  
  }  

3. config.json中:

"imageUrlPrefix": "http://localhost:8080",  
"imagePathFormat": "/myproject/file/image/{yyyy}{mm}{dd}/{time}{rand:6}",

4. 修改tomcat虚拟路径:http://blog.csdn.net/eunyeon/article/details/71124314

<Context path="" docBase="E:\tmp" reloadable="false" ></Context>