ueditor 上傳檔案到ftp java
阿新 • • 發佈:2019-01-09
最近用到 富文字編輯器 發現 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 可能不經常上,有問題加我QQ 695438455
下面直接上配置檔案 直接給大家截圖了
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;
}
到這裡就差不多了。