springMVC+maven+nginx ueditor上传配置详解

  1. 下载Jsp开发版放到项目中。项目使用nginx代理静态文件,所以把ueditor放在static/js目录下。
  2. 添加jsp/lib下的jar包依赖。项目使用maven管理jar包,所以在pom文件中添加相关依赖,其中ueditor的jar需要自己上传到maven仓库。
  3. 获取config.json配置文件。项目使用springMVC,所以把jsp下的controller.jsp改成controller的形式,如下:
@Value("${site.ueditor.path}")
private String rootPath;

@RequestMapping("/ueditor/config")
public void config(HttpServletRequest request, HttpServletResponse response, String action) {
    try {
        request.setCharacterEncoding("utf-8");
        response.setHeader("Content-Type", "text/html");
        //String rootPath = request.getServletContext().getRealPath("/");
        String exec = new ActionEnter(request, rootPath).exec();
        PrintWriter writer = response.getWriter();
        writer.write(exec);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

由于ueditor并未放置在项目根目录下,所以需将rootPath配置为ueditor所在的目录,这样程序会到ueditor所在目录下的ueditor文件夹(controller的映射)下面去找config.json配置文件。修改ueditor.config.js中的serverUrl,请求以上接口就可以获取到配置文件。
4. 请求自己的接口上传文件,如下:

UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function(action) {
    if (action == 'uploadimage' || action == 'uploadvideo') {
        return createUrl('/ueditor/upload');
    } else {  
        return this._bkGetActionUrl.call(this, action);
    }
}

用此方法就不需要修改config.json文件中的imageActionName配置。

@Value("${site.imgs.path}")
private String imgsPath;

@RequestMapping(value = "/ueditor/upload", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> upload(MultipartHttpServletRequest multipartRequest) throws IllegalStateException, IOException {
    MultipartFile multipartFile = multipartRequest.getFile("upfile");
    String original = multipartFile.getOriginalFilename();
    String type = original.substring(original.lastIndexOf("."));
    String fileName = "UED_" + System.currentTimeMillis() + type;
    String filePath = imgsPath + fileName;
    File file = new File(filePath);
    if (!file.exists()) {
        file.mkdirs();
    }
    multipartFile.transferTo(file);
    Map<String, String> result = new HashMap<String, String>(); 
    result.put("state", "SUCCESS");
    result.put("original", original);
    result.put("size", String.valueOf(file.length()));
    result.put("title", fileName);
    result.put("type", type);    
    result.put("url", fileName); 
    return result;
}

需要注意的是接口返回的json数据一定要满足ueditor需要的格式。
5. 图片上传成功后正确的显示。修改config.json文件中的imageUrlPrefix配置为上传图片所在的目录。
附:ueditor初始化配置

var ue = UE.getEditor('container',{
    enableAutoSave: false,
    elementPathEnabled: false,
    wordCount: false,
    autoClearinitialContent: true,
    initialContent: '<p></p>',
    initialFrameHeight: 300,
    saveInterval: 5000000
});
ue.ready(function() {
    ue.setContent('<p></p>');
});
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值