关于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;
	}
}  


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值