ueditor默认的文件上传是依靠commons-fileupload插件,提供上传服务的就是启动了我们项目的tomcat容器。我们了解了ueditor与fastdfs,我们也了解了fastdfs-client,这篇文章我们将阐述整合ueditor与fastdfs。
1、前提约束
- 安装fastdfs https://www.jianshu.com/p/d7a8ea919094
- 熟悉fastdfs-client https://www.jianshu.com/p/8e81bd70504b
- 熟悉ueditor https://www.jianshu.com/p/acaec1f27751
- 安装nginx https://www.jianshu.com/p/a888c35daa81
2、创建新项目
以下图所示方式创建【去掉ueditor jar包,加入fastdfs_client-1.25.jar,导入去掉的jar包的源码,功能不受任何影响,但却为我们文件修改上传方式提供了前提】:
3、修改源代码
- 在com.baidu.ueditor.define.State.java中增加一个方法声明getInfo
String getInfo(String name);
因为该接口多增加一个方法,那么接口的实现类也要增加该方法实现:
在com.baidu.ueditor.define.MultiState.java以及com.baidu.ueditor.define.BaseState.java中增加方法实现:
@Override
public String getInfo(String name) {
return this.infoMap.get(name);
}
- 修改web/jsp/下的config.json的imageUrlPrefix值【注意:以前是空串】
"imageUrlPrefix": "http://192.168.100.192/", /* 图片访问路径前缀 */
- 在src/下增加trackerClient.conf
tracker_server=192.168.100.192:22122
- 修改com.baidu.ueditor.upload.StorageManager.java中的saveTmpFile(),并在其中增加一个静态代码块用来做一次性加载配置文件
static{
try {
ClientGlobal.init(new URLDecoder().decode(StorageManager.class.getClassLoader().getResource("/").getPath())+"/"+"trackerClient.conf");
}
catch(Exception e)
{
}
}
private static State saveTmpFile(File tmpFile, String path) {
State state = null;
File targetFile = new File(path);
if (targetFile.canWrite()) {
return new BaseState(false, AppInfo.PERMISSION_DENIED);
}
/*try {
FileUtils.moveFile(tmpFile, targetFile);
} catch (IOException e) {
return new BaseState(false, AppInfo.IO_ERROR);
}*/
//保存图片到服务器上========================================
try {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String[] strings = storageClient.upload_file(tmpFile.getPath(), FileType.getSuffixByFilename(targetFile.getName()).substring(1), null);
String url = strings != null ? strings[0] + "/" + strings[1] : null;
state = new BaseState(true);
state.putInfo("url",url);
}
catch(Exception e)
{
e.printStackTrace();
}
//保存图片到服务器上========================================
return state;
}
- 修改com.baidu.ueditor.upload.BinaryUploader.java【位置大约在save(HttpServletRequest request,Map<String, Object> conf)方法的最后几行】
将
if (storageState.isSuccess()) {
storageState.putInfo("url", PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
}
改为
if (storageState.isSuccess()) {
storageState.putInfo("url", storageState.getInfo("url"));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
}
4、修改nginx.conf,并且重启nginx
在http的server节点中增加一个location映射
location /group1/M00 {
#映射逻辑路径到物理路径,倘若url中出现"/group1/M00",则替换为"/root/docker/fastdfs/storage/data"
alias /root/docker/fastdfs/storage/data;
}