后台文章模块用了ueditor编辑器,可以上传图片和附件,在windows环境开发时没有发现问题,但昨天想写一篇BLOG时发现上传功能不好使了。

image.png

我的项目架构:SpringBoot+Ueditor


参考很详细的SpringBoot整合UEditor教程将UEditor集成进SpringBoot内。


出现的问题:windows可以上传,linux提示如上图。


解决方法直接看最后。

解决方法直接看最后。

解决方法直接看最后。


排查:

1.在本地访问localhost/config?action=config

返回:

{"videoMaxSize":102400000,"videoActionName":"uploadvideo","fileActionName":"uploadfile","fileManagerListPath":"/ueditor/jsp/upload/file/","imageCompressBorder":1600,"imageManagerAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"imageManagerListPath":"/ueditor/jsp/upload/image/","fileMaxSize":51200000,"fileManagerAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp",".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid",".rar",".zip",".tar",".gz",".7z",".bz2",".cab",".iso",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".md",".xml"],"fileManagerActionName":"listfile","snapscreenInsertAlign":"none","scrawlActionName":"uploadscrawl","videoFieldName":"upfile","imageCompressEnable":true,"videoUrlPrefix":"","fileManagerUrlPrefix":"","catcherAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"imageManagerActionName":"listimage","snapscreenPathFormat":"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}","scrawlPathFormat":"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}","scrawlMaxSize":2048000,"imageInsertAlign":"none","catcherPathFormat":"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}","catcherMaxSize":2048000,"snapscreenUrlPrefix":"","imagePathFormat":"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}","imageManagerUrlPrefix":"","scrawlUrlPrefix":"","scrawlFieldName":"upfile","imageMaxSize":2048000,"imageAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"snapscreenActionName":"uploadimage","catcherActionName":"catchimage","fileFieldName":"upfile","fileUrlPrefix":"","imageManagerInsertAlign":"none","catcherLocalDomain":["127.0.0.1","localhost","img.baidu.com"],"filePathFormat":"/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}","videoPathFormat":"/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}","fileManagerListSize":20,"imageActionName":"uploadimage","imageFieldName":"upfile","imageUrlPrefix":"","scrawlInsertAlign":"none","fileAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp",".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid",".rar",".zip",".tar",".gz",".7z",".bz2",".cab",".iso",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".md",".xml"],"catcherUrlPrefix":"","imageManagerListSize":20,"catcherFieldName":"source","videoAllowFiles":[".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid"]}

返回config.json的配置内容

2.部署linux后访问相同的接口:changex.site/config?action=config

返回:配置文件初始化失败

3.打开idea全局搜索:”配置文件初始化失败“,发现:

image.png

4.查看CONFIG_ERROR这个错误码在哪些地方使用过:

image.png

5.点进去发现只有一处调用:

image.png

6.很显然在linux上configManager没有被正常加载,导致为null,接下来我们需要知道两点:(1)configManager在什么时候加载的(2)为什么没有加载成功

7.首先我们继续查看invoke()这个方法在哪儿被使用过,发现只在ActionEnter.exec()有调用过该方法,在查看exec()方法在哪儿调用的会发现

image.png

8.查看构造函数,找到configManager初始化的地方了!

image.png

9.一路点进去查看:

image.png

10.这个方法是用于获取config.json的绝对地址的,按照上面那篇文章改写之后是:

image.png

11.问题肯定是在这里,这里没有正确读取到config.json的内容,导致configContent为null,我们给他修改一下

image.png

12.需要修改的地方有两处:ConfigManager.readFile(String path)和ConfigManager.getConfigPath(),修改之后的代码:

private InputStream getConfigPath() {

    //获取config.json的输入流
    return this.getClass().getClassLoader().getResourceAsStream("config.json");
}

private String readFile(InputStream in) throws IOException {

    StringBuilder builder = new StringBuilder();

    try {

        InputStreamReader reader = new InputStreamReader(in, "UTF-8");
        BufferedReader bfReader = new BufferedReader(reader);

        String tmpContent = null;

        while ((tmpContent = bfReader.readLine()) != null) {
            builder.append(tmpContent);
        }

        bfReader.close();

    } catch (UnsupportedEncodingException e) {
        // 忽略
    }

    return this.filter(builder.toString());

}

13.再次部署到linux访问changex.site/config?action=config 能够正确显示配置文件信息,上传功能自然也好啦~