UEditor之基于Java图片上传前后端源码研究

开心一笑

一定要快乐学习,所以学习之前先看个笑话:

刚来北京,租了一个小房,一楼,上淘宝买衣服,选了付钱了联系卖家:“我已付款,请发货。”谁知那货直接说:“我看到你地址了,自己上楼来拿吧!我就在你楼上。”
拿你妹,老子付了邮费的。。。送下来。

视频教程

CSDN学院:

http://edu.csdn.net/lecturer/994

腾讯学院:

https://huangwy.ke.qq.com/

网易学院:

http://study.163.com/instructor/1035331499.htm

提出问题

Ueditor前后端源码的学习和简单的研究???

解决问题

前提:

  • 假如你已经看了我的前一篇文章,这一点很重要的啊,当然不看也可以,因为你已经是一个高手,就像我一样,哈哈;
  • 假如你已经安装tomcat服务器;
  • 假如你已经把项目运行到Eclipse上;
  • 假如你已经有java基础;
  • 假如你对js有一定基础;
  • 假如你已经下载ueditor1_4_3_3-src源码,记得是开发的哦;

那么开始吧!

这是我的项目目录

这里写图片描述

1.从访问路径http://localhost:8081/Test/_examples/simpleDemo.html,我们主要是要看看,富文本框被加载出来之前,会调用哪些代码,

不卖关子,很明显,会调用后端的controller.jsp代码,因为我们已经在ueditor.config配置了:

// 服务器统一请求接口路径
, serverUrl: URL + "jsp/controller.js

看看controller.jsp代码,上一篇文章我们已经讲了,要把这些代码看作是后端代码,很重要很重要的:

<%@ page language="java" contentType="text/html; charset=UTF-8"
import="com.baidu.ueditor.ActionEnter"
pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%

    request.setCharacterEncoding( "utf-8" );
    response.setHeader("Content-Type" , "text/html");
    /** 项目根路径 **/
    String rootPath = application.getRealPath( "/" );
    /** 调用后端的ActionEnter类,并执行exec方法 **/
    out.write( new ActionEnter( request, rootPath ).exec() );

%>

我们就到ActionEnter.java看看吧,这个类就是前端调用后端的唯一入口,也只有这个入口了,记住第一章有讲了,要把源码复制到src下,进行调试哦!不知道先看第一章吧!!!!!

package com.baidu.ueditor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.baidu.ueditor.hunter.FileManager;
import com.baidu.ueditor.hunter.ImageHunter;
import com.baidu.ueditor.upload.Uploader;

public class ActionEnter {

private HttpServletRequest request = null;

private String rootPath = null;
private String contextPath = null;

private String actionType = null;

private ConfigManager configManager = null;
/** action统一入口 **/
public ActionEnter ( HttpServletRequest request, String rootPath ) {

    this.request = request;
    /** rootPath = /Test/ **/
    this.rootPath = rootPath;
    /** actionType = config **/
    this.actionType = request.getParameter( "action" );
    /** contextPath = /Test **/
    this.contextPath = request.getContextPath();
    /** 调用ConfigManager **/
    this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI() );

}

2.ConfigManager类主要用来读取后端的配置文件,就是config.json这个文件,事实上这个文件应该放在后端的。

/**
 * 配置管理器
 * @author hancong03@baidu.com
 *
 */
public final class ConfigManager {

private final String rootPath;
private final String originalPath;
private final String contextPath;
/** 存放备注文件 **/
private static final String configFileName = "config.json";
private String parentPath = null;
private JSONObject jsonConfig = null;
// 涂鸦上传filename定义
private final static String SCRAWL_FILE_NAME = "scrawl";
// 远程图片抓取filename定义
private final static String REMOTE_FILE_NAME = "remote";

/*
 * 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件
 */
private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException {

    rootPath = rootPath.replace( "\\", "/" );
    //下面的rootPath就是我的根路径
    // rootPath=D:/workspace_de_client/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Test/
    this.rootPath = rootPath;
    this.contextPath = contextPath;
    //请求路径 url = /Test/jsp/controller.jsp
    if ( contextPath.length() > 0 ) {
        // D:/workspace_de_client/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Test//jsp/controller.jsp
        this.originalPath = this.rootPath + uri.substring( contextPath.length() );
    } else {
        this.originalPath = this.rootPath + uri;
    }
    /** 调用当前类的初始化环境方法 initEnv **/
    this.initEnv();

}

//上面的方法无非就是获得controller.jsp这个类所在的真实目录而已


//下面看看initEnv()这个方法

private void initEnv () throws FileNotFoundException, IOException {
    /**  **/
    File file = new File( this.originalPath );

    if ( !file.isAbsolute() ) {
        file = new File( file.getAbsolutePath() );
    }
    /** 获得文件的父路径,也就是  ..../jsp **/
    this.parentPath = file.getParent();
    /** 读取配置文件,这个方法比较重要,往下看 **/
    String configContent = this.readFile( this.getConfigPath() );

    try{
        /** 把返回的的json字符串扔进JsonObject对象中 **/
        JSONObject jsonConfig = new JSONObject( configContent );
        this.jsonConfig = jsonConfig;
    } catch ( Exception e ) {
        this.jsonConfig = null;
    }

}

/** 获得配置路径,记住config.json是和controller.jsp放在同一个目录下的,很坑有木有 **/
private String getConfigPath () {
    /** 拼凑config.json的真实路径 **/
    return this.parentPath + File.separator + ConfigManager.configFileName;
}

private String[] getArray ( String key ) {

    JSONArray jsonArray = this.jsonConfig.getJSONArray( key );
    String[] result = new String[ jsonArray.length() ];

    for ( int i = 0, len = jsonArray.length(); i < len; i++ ) {
        result[i] = jsonArray.getString( i );
    }

    return result;

}
/** 获得配置文件的内容,变成字符串返回 **/
private Str
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@Map

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值