ueditor spring mvc 集成配置

1.进入 ueditor-springmvc-master 目录下 D:\mingruiren\富文本\ueditor-springmvc-master\ueditor-springmvc-master

2.执行 mvn install  安装控件所需依赖jar包

3.复制jsp/config.json 文件到classpath类路径下   也就是maven工程resources目录下 


<dependency>
    <groupId>com.peachyy</groupId>
    <artifactId>ueditor-springmvc</artifactId>
    <version>1.0</version>
</dependency>



4.准备工作完成,开始配置utf8-jsp/ueditor.config.js

window.UEDITOR_HOME_URL="/static/utf8-jsp/";
var URL = window.UEDITOR_HOME_URL || getUEBasePath();

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

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

    ,imageUrl: "" //图片上传提交后台对应的地址,路径固定为*/controller.*
    ,imagePath: "" //图片在服务器上的存储目录
    ,imageFieldName: "" //后台对应接收image的参数名

    // 服务器统一请求接口路径
    , serverUrl: URL
resources/config.json

/* 上传图片配置项 */
"imageActionName": "/static/utf8-jsp/upload", /* 执行上传图片的action名称 */
"imageFieldName": "upfile", /* 提交的图片表单名称 */
"imageMaxSize": 2048000, /* 上传大小限制,单位B */
"imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
"imageCompressEnable": true, /* 是否压缩图片,默认是true */
"imageCompressBorder": 1600, /* 图片压缩最长边限制 */
"imageInsertAlign": "none", /* 插入的图片浮动方式 */
"imageUrlPrefix": "/static/utf8-jsp/", /* 图片访问路径前缀 */
"imagePathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
                            /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */
                            /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */
                            /* {time} 会替换成时间戳 */
                            /* {yyyy} 会替换成四位年份 */
                            /* {yy} 会替换成两位年份 */
                            /* {mm} 会替换成两位月份 */
                            /* {dd} 会替换成两位日期 */
                            /* {hh} 会替换成两位小时 */
                            /* {ii} 会替换成两位分钟 */
                            /* {ss} 会替换成两位秒 */
                            /* 非法字符 \ : * ? " < > | */
                            /* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */


扫描二维码关注公众号,回复: 1749054 查看本文章

4.新建SpringMVCUEditorSupport  提供加载配置方法,该类继承的父类就是读取类路径下的config.json文件

/**
 * User: MingRuiRen
 * Date: 2017/7/19 16:18
 * Description:
 */
@Controller
@RequestMapping("/static/utf8-jsp")
public class SpringMVCUEditorSupport extends SpringMvcUeditorSupport
{

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> upload(HttpServletRequest request,@RequestParam CommonsMultipartFile upfile) throws
        IOException
    {
        Map<String, String> result = Maps.newHashMap();
        System.out.println(upfile.getFileItem().getFieldName());
        String path = getFilePath(upfile);
        File file = new File(path);
        System.out.println(path);
        String state = "SUCCESS";
        //返回类型
        String rootPath = request.getContextPath();
        result.put("url", rootPath + "/ueditor/show?filePath=" + path);
        result.put("size", String.valueOf(file.length()));
        result.put("type", file.getName().substring(file.getName().lastIndexOf(".")));
        result.put("state", state);
        return result;
    }


    @RequestMapping(value = "/ueditor/show", method = RequestMethod.GET)
    public void show(String filePath, HttpServletResponse response) throws IOException {

        File file = getFile(filePath);

        response.setDateHeader("Expires", System.currentTimeMillis() + 1000 * 60 * 60 * 24);
        response.setHeader("Cache-Control", "max-age=60");
        OutputStream os = response.getOutputStream();

        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            IOUtils.copy(is, os);
        } catch (FileNotFoundException e) {
            response.setStatus(404);
            return;
        } finally {
            if (null != is) {
                is.close();
            }
            if (null != os) {
                os.flush();
                os.close();
            }
        }
    }

    protected String getFilePath(CommonsMultipartFile uploadFile){
        String absolutePath = "D:/upload";
        File folder = new File(absolutePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String rawName = uploadFile.getFileItem().getName();
        String fileExt = rawName.substring(rawName.lastIndexOf("."));
        String newName = System.currentTimeMillis() + UUID.randomUUID().toString() + fileExt;
        File saveFile = new File(absolutePath + File.separator + newName);
        try {
            uploadFile.getFileItem().write(saveFile);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return absolutePath + "/" + newName;
    }

    protected File getFile(String path){
        File file = new File(path);
        return file;

    }



猜你喜欢

转载自blog.csdn.net/u014755645/article/details/75462831
今日推荐