最近因为项目中的需要,需要将管理后台Fckeditor上传的图片上传到统一的图片服务器(apache实现)中,取代现有上传保存到自身项目环境下~~ 调试代码过程将代码锁定到以下两个方法: (1)、net.fckeditor.connector.ConnectorServlet#doPost() (2)、net.fckeditor.tool.UtilsResponse#constructResponseUrl() 具体参数意义建议DEBUG设置断点的情况下,调研一下~~会发现一目了然改动第一步,设置远程上传,这个其实不是难点,ConnectorServlet#doPost() 最后部分添加将pathToSave该文件SFTP给图片服务器的功能。 (SFTP代码下次有时间的时候贴出~~) 使用过fckeditor的同仁应该都会注意到上传之后,input框中地址,也就是这个相对路径会被保存到数据库或者代码中~~,因此我们改动的下一步就是让上传图片之后,input框中地址显示的是绝对路径,也就是图片服务器地址 DEBUG模式下可以发现实现上传之后,会触发一个JS操作,切换DIV,以及相应的onComplete事件(单纯从页面和JS中很难找到,因为是通过IO流实现的,DEBUG下可以从IO流中发现) 这个RESPONSE是通过UtilsResponse的constructResponseUrl构建的,因此改动点出现了,在fckeditor.properties中添加一个属性,java代码去获取这个属性,如果配置了远程服务器,那直接返回图片服务器的地址,反之,继续原先的操作,返回相对项目图片保存路径
static String remoteFilePath = null;
static final String configureFile = "fckeditor.properties";
static {
Properties properties = PropertiesUtil.getProperties(configureFile);
remoteFilePath = properties.getProperty("connector.remoteFilePath");
}
public static String constructResponseUrl(HttpServletRequest request, ResourceTypeHandler resourceType, String urlPath, boolean prependContextPath, boolean fullUrl)
{
if (remoteFilePath == null || remoteFilePath.trim().length() == 0) {
StringBuffer sb = new StringBuffer();
if (fullUrl) {
String address = request.getRequestURL().toString();
sb.append(address.substring(0, address.indexOf(47, 8)) + request.getContextPath());
}
if ((prependContextPath) && (!(fullUrl)))
sb.append(request.getContextPath());
sb.append(ConnectorHandler.getUserFilesPath(request));
sb.append(resourceType.getPath());
if (Utils.isNotEmpty(urlPath))
sb.append(urlPath);
return sb.toString();
}
return remoteFilePath;
}
#fckeditor.properties connector.remoteFilePath=http://images.XXXX.com/images/
将修改后的代码重新打包一下,重新调试一下,OK,搞定收工~~~