關於uEditor,上傳圖片到指定主機(圖片服務器)

jsp、html中如下:

	<div class="editor_wrap">
		<script id="editor" type="text/plain" style="width:1000px;height:500px;"></script>
	</div>


在js中如下:

UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function(action){
    	//調用自己寫的Controller
    	if(action == 'uploadimage' || action == 'uploadfile'){
    		return "${ctx}/ueditor/imageUp"; //自己controller的action
    	}else if(action == "uploadvideo"){
    		return "${ctx}/ueditor/videoUp";//自己controller的action
    	}else{
    		return this._bkGetActionUrl.call(this,action);//百度編輯器默認的action
    	}
    }
	var ue = UE.getEditor('editor');

自己的controller:   ueEditor會將圖片、視頻傳到自定義的controller中,再處理就OK了。

package com.pd.*****.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.pd.***.entity.URlAndPath;
import com.pd.***.util.FileUploadUtils;
import com.pd.***.utils.PropertiesUtils;
/**
 * Ue便捷器文件上傳Controller
 * @author guoxinming
 * @date  2017-07-18
 */  
@Controller  
@RequestMapping("/ueditor")  
public class UeditorController {  
    /**
     * 上傳圖片到圖片服務器
     * @param file 上傳文件流
     * @param request 請求域
     * @return String 返回JSON數據
     */
	@RequestMapping(value="/imageUp" ,produces = {"application/json;charset=UTF-8"})
    @ResponseBody  
    public String fileUp(@RequestParam(value = "upfile", required = false) MultipartFile file, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		String url=PropertiesUtils.getProperty("IMAGE_UPLOAD_ROOT")+PropertiesUtils.getProperty("NEWS_PICTURE_PATH")+"/";
		result=fileUpToServer(file,url);
		return JSON.toJSONString(result); 
    }  
	/**
     * 上傳視頻到服務器
     * @param file 上傳文件流
     * @param request 請求域
     * @return String 返回JSON數據
     */
	@RequestMapping(value="/videoUp" ,produces = {"application/json;charset=UTF-8"})
    @ResponseBody  
    public String videoUp(@RequestParam(value = "upfile", required = false) MultipartFile file, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		String url=PropertiesUtils.getProperty("IMAGE_UPLOAD_ROOT")+PropertiesUtils.getProperty("NEWS_VIDEO_PATH")+"/";
		result=fileUpToServer(file,url);
		return JSON.toJSONString(result); 
    }  
	/**
	 * 上傳文件到服務器
	 * @param file
	 * @param  url 文件上傳服務器的路徑
	 * @return Map<String,Object> 對應的文件回顯數據
	 */
	public Map<String,Object> fileUpToServer(MultipartFile file,String url){
		String fileName = file.getOriginalFilename();// 獲得上傳文件的實際名稱
		String[] types = fileName.split("\\.");
		String type = types[types.length - 1];
		type = "." + type;// 獲得文件的後綴名

		List<URlAndPath> list = new ArrayList<URlAndPath>();
		FileUploadUtils.uploadPic(file, list, url);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("state", "SUCCESS");// UEDITOR的規則:不爲SUCCESS則顯示state的內容
		result.put("url", list.get(0).getUrl());
		result.put("title", file.getOriginalFilename());
		result.put("original", file.getOriginalFilename());
		result.put("size", file.getSize());
		result.put("type", type);
		return result;
	}
}  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章