SSM整合百度富文本编辑器ueditor二

传动门
http://blog.csdn.net/qq_34371461/article/details/78679993
正题:
修改config.json中的文件上传配置

"imageUrlPrefix": "http://images.test.com", /* 图片访问路径前缀(图片服务器地址) */
"imagePathFormat": "/{yyyy}{mm}{dd}/{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
"physicsPath":"D:/www/images", /* 文件存放的物理路径,window服务器 */

ConfigManager类修改如下:

    /**
     * 通过一个给定的路径构建一个配置管理器,该管理器要求地址路径必须存在config.properties文件中
     *
     * @param rootPath
     * @param contextPath
     * @param uri
     * @throws FileNotFoundException
     * @throws IOException
     */
    private ConfigManager(String rootPath, String contextPath, String uri)
            throws FileNotFoundException, IOException {
        rootPath = rootPath.replace("\\", "/");
        this.rootPath = rootPath;
        this.contextPath = contextPath;

        //注释原有的读取配置文件方式
//        if (contextPath.length() > 0)
//          this.originalPath = (this.rootPath + "WEB-INF/classes/config.json");
//        else {
//          this.originalPath = (this.rootPath + uri);
//        }
        /**指定新的配置文件读取路径,确保ueditor在初始化能够正确的加载配置文件*/
        this.originalPath = this.rootPath + "WEB-INF/classes/config.json";

        initEnv();
    }

其中文件上传用了BinaryUploader类,一开始上传图片并没有成功,debug模式下跟踪发现问题所在,
在BinaryUploader中:

/**debug到这里发现从request解析数据为空*/
FileItemIterator iterator = upload.getItemIterator(request);

一番研究,原因是因为项目中用的SpringMVC的缘故,spring mvc会把request中的文件封装为对象MultipartFile,所以springmvc会先对request进行解析,这里你再进行解析就获取不到了。解决办法,找到springmvc中关于文件上传的配置:

<bean id="multipartResolver" 
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600" />
        <property name="maxInMemorySize" value="4096" />
        <property name="defaultEncoding" value="UTF-8"></property>
</bean>

可以看到上传用到了CommonsMultipartResolver类,我们自己写个类继承它:

public class CommonsMultipartResolverUeditor extends CommonsMultipartResolver {
    @Override
    public boolean isMultipart(HttpServletRequest request)
    {
        String url = request.getRequestURI();
        //对所有请求的上传路径做个匹配,如果是编辑器的请求那么不用springmvc的上传处理
        if (url != null && url.contains("/sys/ueditor/exec"))
        {
            return false;
        }
        else
        {
            //springmvc的上传处理
            return super.isMultipart(request);
        }
    }
}

修改配置为:

<!-- 配置springMVC处理上传文件的信息 -->
    <bean id="multipartResolver"    
        class="com.hooenergy.operator.api.model.entity.ueditor.CommonsMultipartResolverUedito">
        <property name="maxUploadSize" value="104857600" />
        <property name="maxInMemorySize" value="4096" />
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

这样就能正常上传图片了。
其中上传类还有一些修改如下:

public class BinaryUploader {
    public static final State save(HttpServletRequest request, Map<String, Object> conf) {
        FileItemStream fileStream = null;
        boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;

        /**
         * 请求中是否包含内容
         */
        if (!ServletFileUpload.isMultipartContent(request)) {
            return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
        }

        /**FileItem--每个FileItem的实例都包含一个文件以及该文件的其他一些属性(文件名、大小等)*/
        /**DiskFileItemFactory——工厂类*/
        /**ServletFileUpload--用于解析resquest,能从resquest中解析出装有FileItem对象的一个List*/
        ServletFileUpload upload = new ServletFileUpload(
                new DiskFileItemFactory());

        if (isAjaxUpload) {
            upload.setHeaderEncoding("UTF-8");
        }
        try {

            //解析request请求 返回FileItemStream的iterator实例,spring mvc会把request中的文件封装为对象MultipartFile
            FileItemIterator iterator = upload.getItemIterator(request);

            while (iterator.hasNext()) {
                fileStream = iterator.next();

                if (!fileStream.isFormField())
                    break;
                fileStream = null;
            }

            if (fileStream == null) {
                return new BaseState(false, 7);
            }

            /** 上传保存路径,可以自定义保存路径和文件名格式 config.json: filePathFormat */
            String savePath = (String) conf.get("savePath");
            String originFileName = fileStream.getName(); // 111.jpg
            String suffix = FileType.getSuffixByFilename(originFileName); // .jpg 后缀

            originFileName = originFileName.substring(0,
                    originFileName.length() - suffix.length()); // 111 文件名
            savePath = savePath + suffix; // /ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{rand:6}.png 可以统一采用时间戳方法命名

            long maxSize = ((Long) conf.get("maxSize")).longValue();

            if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
                return new BaseState(false, 8);
            }

            savePath = PathFormat.parse(savePath, originFileName);

            /** 类似 D:/Program Files/apache-tomcat-7.0.64/wtpwebapps//ueditor/jsp/upload/image/20151116/1447675575987060166.png */
//            String physicalPath = (String) conf.get("rootPath") + savePath;  // 默认配置图片上传地址是在项目路径下,项目重新部署图片就没有了,所以这里我们采用自己配置的保存路径,保存到服务器的其他盘下

            /****************修改文件存放路径为自己配置的存放路径*****************/
            String physicalPath = (String) conf.get("physicsPath");
            if (physicalPath != null && !"".equals(physicalPath)) {
                physicalPath += savePath;
            } else {
                physicalPath = (String) conf.get("rootPath") + savePath;
            }
            /*************************************************************/

            InputStream is = fileStream.openStream();
            //State这个类很重要,是一个接口,它是返回到前端的数据
            State storageState = StorageManager.saveFileByInputStream(is,
                    physicalPath, maxSize);
            is.close();

            if (storageState.isSuccess()) {
                storageState.putInfo("url", PathFormat.format(savePath));
                storageState.putInfo("type", suffix);
                storageState.putInfo("original", originFileName + suffix);
            }

            return storageState;
        } catch (FileUploadException e) {
            return new BaseState(false, 6);
        } catch (IOException localIOException) {
        }
        return new BaseState(false, 4);
    }

    private static boolean validType(String type, String[] allowTypes) {
        List list = Arrays.asList(allowTypes);

        return list.contains(type);
    }
}

到这里已经大功告成了,如果你还有什么问题或不明白的地方,欢迎评论留言。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值