最近用到 富文本编辑器 发现 ueditor 界面还挺漂亮的,就选择用它了,由于默认的上传图片是保存到 tomcat/webapps/项目下面的,这样肯定是不合适的,万一重新部署,之前的
数据就有可能丢失了,所以呢想着把图片文件传到ftp下做永久保存,但是问题来了,ue没提供这种办法,只能自己拓展了,好在是开源的就方便多了,在这里特别感谢一下
先说一下原理
1、统一入口是 controller.jsp这个文件
2、通过解析config.json文件进行上传
3、上传完成以后会返回一个json串通过State 来封装源码里面有
格式:
{
"state": "SUCCESS",
"title": "1459149871185023084.png",
"original": "filescan.png",
"type": ".png",
"url": "图片路径",
"size": "4282"
}
前台就会解析到以上信息进行图片预览显示
帅气猪 的文章原文ftp上传
http://www.cnblogs.com/AlexLiu1986/p/4699764.html
稍后会把我本地的实现上传到百度云供大家下载
用这个 无需导入ueditor-1.1.2.jar这个包了
下载地址:http://pan.baidu.com/s/1pKDgkvL
csdn 可能不经常上,可以直接下载源码 https://github.com/charlierui/spring-mvc.git
下面直接上配置文件 直接给大家截图了
config.json 文件
ueditor.config.js 文件修改地方
修改为你自己的项目路径不修改的话页面不会创建成功,ueditor就是官网下载的文件,我给他重命名了。
这两个地方修改完成剩下的就是修改 ue的java源码了
源码结构
一下修改的代码只贴 修改部分,供大家看一下,全部的代码会给大家共享到百度云
先修改 ActionEnter 的invoke()方法
case ActionMap.UPLOAD_IMAGE:
case ActionMap.UPLOAD_SCRAWL:
case ActionMap.UPLOAD_VIDEO:
case ActionMap.UPLOAD_FILE:
conf = this.configManager.getConfig( actionCode );
//用户ftp上传
conf.put("useFtpUpload",this.configManager.getAllConfig().getString("useFtpUpload"));
conf.put("keepLocalFile",this.configManager.getAllConfig().getString("keepLocalFile"));
state = new Uploader( request, conf ).doExec();
break;
接下来修改 Uploader
public final State doExec() {
String filedName = (String) this.conf.get("fieldName");
State state = null;
if ("true".equals(this.conf.get("isBase64"))) {
state = Base64Uploader.save(this.request.getParameter(filedName),
this.conf);
} else {//ftp上传判断
if("true".equals(this.conf.get("useFtpUpload"))){
state = FtpUploadUtilbaidu.save(request, conf);
} else{
state = BinaryUploader.save(this.request, this.conf);//系统默认的上传方法必须带着
}
}
return state;
}
大家可能看到 FtpUploadUtilbaidu 这个是自己创建的,里面的方法和BinaryUploader都一样,只是做了一些修改红色部分
public static final State save(HttpServletRequest request,
Map<String, Object> conf) {
FileItemStream fileStream = null;
boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;
if (!ServletFileUpload.isMultipartContent(request)) {
return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
}
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
if ( isAjaxUpload ) {
upload.setHeaderEncoding( "UTF-8" );
}
try {
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
fileStream = iterator.next();
if (!fileStream.isFormField())
break;
fileStream = null;
}
if (fileStream == null) {
return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
}
String savePath = (String) conf.get("savePath");
String originFileName = fileStream.getName();
String suffix = FileType.getSuffixByFilename(originFileName);
originFileName = originFileName.substring(0,
originFileName.length() - suffix.length());
savePath = savePath + suffix;
long maxSize = ((Long) conf.get("maxSize")).longValue();
if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
}
savePath = PathFormat.parse(savePath, originFileName);
String remoteDir = "";
<span style="color:#ff0000;">int pos = savePath.lastIndexOf("/");
if(pos > -1){
remoteDir = savePath.substring(0,pos + 1);
}</span>
String physicalPath = (String) conf.get("rootPath") + savePath;
//是否上传后保留本地服务器文件config.json,里面的配置
<span style="color:#ff0000;"> boolean keepLocalFile = "false".equals(conf.get("keepLocalFile")) ? false : true;
</span> InputStream is = fileStream.openStream();
//调用自己ftp上传方法
<span style="color:#ff0000;">State storageState = StorageManager.saveFtpFileByInputStream(is, remoteDir,
physicalPath, maxSize, keepLocalFile);</span>
// State storageState = StorageManager.saveFileByInputStream(is,
// physicalPath, maxSize);
is.close();
if (storageState.isSuccess()) {
//这里的默认url是 ue 配置文件config-imagePathFormat属性的路径,这里我需要替换成ftp的路径
//storageState.putInfo("url", PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
}
return storageState;
} catch (FileUploadException e) {
return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
} catch (IOException e) {
}
return new BaseState(false, AppInfo.IO_ERROR);
}
下面提供 ftp 上传方法 ,
<span style="color:#ff0000;">saveFtpFileByInputStream 这个方法就不贴了,下载了到代码找吧</span>
<span style="color:#ff0000;">
</span>
/**
* ftp上传文件
*
* */
private static State saveFtpTmpFile(File tmpFile, String remoteDir, String path,boolean keepLocalFile) {
State state = null;//用来拼装 上传成功后返回的json串
File targetFile = new File(path);
String share_img=null;//得到上传图片路径
if (targetFile.canWrite())
return new BaseState(false, 2);
try
{
FileUtils.moveFile(tmpFile, targetFile);
} catch (IOException e) {
return new BaseState(false, 4);
}
try
{
String module = "imagesjkzx";
String timepoint = new SimpleDateFormat("yyyyMMddHHmmssSSS")
.format(new Date());
String[] param = { null, module, timepoint };
//下面是上传方法,我就不贴了,需求都不一样,具体的ftp上传方法,自己网上找找吧有的是,需要注意的是上传完以后必须能够得到ftp路径
share_img = FtpUploadUtil.ftpUpload(targetFile, param);
System.out.println("share_img"+share_img);
}catch (Exception e) {
System.out.println(e.getMessage());
return new BaseState(false, 4);
}
try
{
if(! keepLocalFile)
targetFile.delete();
}catch(Exception e){
}
state = new BaseState(true);
state.putInfo("url", share_img);//刚才注释了一个url,现在我是在这里赋值的,share_img就是ftp的路径
state.putInfo("size", targetFile.length());
state.putInfo("title", targetFile.getName());
return state;
}
到这里就差不多了。