UEditor扩展上传

1.修改配置文件ueditor.config.js,重写后台请求方法,serverUrl

if(!window.UEDITOR_HOME_URL){
		window.UEDITOR_HOME_URL = "/lw-component/component/module/ueditor/";
	}
	
    var URL = window.UEDITOR_HOME_URL || getUEBasePath();

    /**
     * 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
     */
    window.UEDITOR_CONFIG = {

        //为编辑器实例添加一个路径,这个不能被注释
        UEDITOR_HOME_URL: URL

        // 服务器统一请求接口路径
        , serverUrl: "/xxx-component/component/ueditor/exec"


2.对post/get传递的进行重写


@Controller
@RequestMapping("/component/ueditor")
public class UEditorController extends SpringController {
	
	@RequestMapping(value = "/exec", method = RequestMethod.GET)
	@ResponseBody
	public void exec(){
		String jsonFilePath=getRequest().getServletContext().getRealPath(UEditorConstant.CONFIG_FILEPATH);
		String actionType = getRequest().getParameter("action");
		if(actionType.equals(UEditorConstant.CONFIG)){
			this.print(UEditorUtil.getAllConfig(jsonFilePath));			
		}else{
			Map<String, Object> config = UEditorUtil.getConfig(actionType, jsonFilePath);
			this.print(JsonUtil.map2Json(config));
		}
	}
	
	
	@RequestMapping(value = "/exec", method = RequestMethod.POST)
	@ResponseBody
	public void exec(@RequestParam(value = "upfile") MultipartFile file){
		String jsonFilePath=getRequest().getServletContext().getRealPath(UEditorConstant.CONFIG_FILEPATH);
		String actionType = getRequest().getParameter("action");
		
		if(actionType.equals(UEditorConstant.CONFIG)){
			this.print(UEditorUtil.getAllConfig(jsonFilePath));
		}else{
			Map<String, Object> returnMsg=new HashMap<String, Object>();
			//上传图片
			String filename = file.getOriginalFilename();
			returnMsg.put("original", filename);
			returnMsg.put("title", filename);
			String filePath=UEditorConstant.STORE_DIR_UEDITOR+AttachmentUtil.getRelativePath();
			File dir=new File(filePath);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			String newFileName = UUID.getUUID() + "." + FilenameUtils.getExtension(filename);
			File newFile = new File(dir+ "/"+ newFileName);
			try {
				//保存图片
				file.transferTo(newFile);
				returnMsg.put("state", "SUCCESS");
				//TODO
				returnMsg.put("url", "");
			} catch (IOException e) {			
				e.printStackTrace();
			}
			this.print(JsonUtil.map2Json(returnMsg));
		}
	}


3.常量参数

public class UEditorConstant {
	//初次加载配置
	public static final String CONFIG = "config";
	//上传图片
	public static final String UPLOAD_IMAGE = "uploadimage";
	//上传涂鸦
	public static final String UPLOAD_SCRAWL = "uploadscrawl";
	//上传视频
	public static final String UPLOAD_VIDEO = "uploadvideo";
	//上传文件
	public static final String UPLOAD_FILE = "uploadfile";
	//抓图截图
	public static final String CATCH_IMAGE = "uploadimage";
	//列出文件
	public static final String LIST_FILE = "listfile";
	//列出图片
	public static final String LIST_IMAGE = "listimage";
	
	//配置文件的路径
	public static final String CONFIG_FILEPATH = "/component/module/ueditor/jsp/config.json";
	
	//富文本图片的保存地址
	public static final String STORE_DIR_UEDITOR = "D:"+  "/"  +"upload"+ "/" +"ueditor"+ "/";
}

4.方法:从文件中获取配置信息,转成json对象

public class UEditorUtil {
	
	public static String getAllConfig(String jsonFilePath){
		return readJsonFile(jsonFilePath);
	}
	
	public static Map<String, Object> getConfig(String type,String jsonFilePath) {
		JSONObject jsonConfig=JSONObject.parseObject(readJsonFile(jsonFilePath));
		Map<String, Object> conf = new HashMap<String, Object>();
		if(type.equals(UEditorConstant.UPLOAD_IMAGE)){//上传图片
			conf.put("imageMaxSize", jsonConfig.getLong("imageMaxSize"));
			conf.put("imageAllowFiles", jsonConfig.getJSONArray("imageAllowFiles"));
			conf.put("imageFieldName", jsonConfig.getString("imageFieldName"));
			conf.put("imageCompressEnable", jsonConfig.getBoolean("imageCompressEnable"));			
			conf.put("imageCompressBorder", jsonConfig.getInteger("imageCompressBorder"));			
		}
		return conf;
	}
	
	
	private static String readJsonFile(String path){
		StringBuilder builder = new StringBuilder();
		try {
			InputStreamReader reader = new InputStreamReader(new FileInputStream(path));
			BufferedReader bufferedReader = new BufferedReader(reader);	
			String tempContent = null;
			while ((tempContent=bufferedReader.readLine())!=null) {
				builder.append(tempContent);
			}
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return filter(builder.toString());
	}
	private static String filter(String input){
		return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值