最近发现Ueditor 1.4.3 jsp utf-8版,存在图片上传功能不能直接使用的Bug。在网上查找有解决方法,但还是存在一定的问题。
方法一:
可参考:http://blog.sina.com.cn/s/blog_002e20310102vbz6.html
修改com.baidu.ueditor.hunter.FileManager类下的getPath方法,源代码:
private String getPath ( File file ) { String path = file.getAbsolutePath(); return path.replace(this.rootPath,"\\"); }
修改为:
1 private String getPath ( File file ) { 2 3 String path = file.getAbsolutePath(); 4 5 String rootPath2 = this.rootPath;
//这里的作用是给rootPath最后添加路径分隔符,而在后面替换时将整个路径替换为"\",其实也可以不添加,但在后面替换的时候要替换成"".
//不然的话,最后出来的json数据中会以"//u/r/...",而导致多出http://u/r/...而无法拿到数据 6 if(!rootPath2.endsWith("\\/")){ 7 rootPath2+="/"; 8 } 9 String tempPath = rootPath2.replaceAll("\\/", "\\\\"); 10 String finalPath = path.replace(tempPath,"\\"); 11 return finalPath; 12 13 }
这样写,可以显示同一站点下的图片。
虽然,这样能解决问题,但总感觉不太对劲。后来仔细看代码,原来在源代码中,将rootPath的路径转了下导致后来的路径分隔符不一致而无法替换rootPath
在ConfigManager这个类中:
private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException { rootPath = rootPath.replace( "\\", "/" );
这在mac及linux下应该是没有问题的,但在windows下会存在bug,故可以修改成:
private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException { //rootPath = rootPath.replace( "\\", "/" ); String fileSparator = System.getProperty("file.separator");
//这里的作用是给rootPath最后添加路径分隔符,而在后面替换时将整个路径替换为"\"。
//不然的话,最后出来的json数据中会以"//u/r/...",而导致多出http://u/r/...而无法拿到数据
if(!rootPath.endsWith(fileSparator)){
rootPath+=fileSparator;
}
this.rootPath = rootPath;
只修改这一处地方,其他地方保持不变。返回结果为:
{"state": "SUCCESS","total": 60,"start": 0, list: [{"state": "SUCCESS","url": "/u/r/images/20141129/1417256636864029401.png"},{"state": "SUCCESS","url": "/u/r/images/jgsz.bmp"},{"state": "SUCCESS","url": "/u/r/images/nh/a.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/b.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/c.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/d.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/e.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/f.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/g.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/h.gif"},{"state": "SUCCESS","url": "/u/r/images/nh/i.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/j.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/k.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/l.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/m.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/n.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/o.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/p.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/_vti_cnf/a.jpg"},{"state": "SUCCESS","url": "/u/r/images/nh/_vti_cnf/b.jpg"} ]}