springMVC百度编辑器虚拟路径配置



 

在springmvc那边如何将附件从web项目分离,参考了网上的一些资料,必需得修改百度编辑器jar的源码才能做到,也不能说改,只能做追加这样可以不破坏他的底层,我这里是用ueditor-1.1.1.jar,其他搞版本也差不多。针对虚拟路径配置然后自己做了下面修改 ,测试过没有问题

1.首先配置好文件上传路径的目录

我本地配置如下:springmvc-servlet.xml

<mvc:resources  location="file:F:\\data\\\apache-tomcat-7.0.23\upload\\webschool\\" mapping="/fileupload/**"  />

2.修改ueditor 的配置环境,打开百度编辑器config.json文件,有四个参数需要做配置:

         imageUrlPrefix参数是界面读取时候的前缀路径,我这边是项目名+fileulpoad(这个是第一点那边配置的) 

         imageUsingVirtualPath 是否使用虚拟路径映射,设置为yes时开启

         imageRealMappingPath  这就是虚拟路径 虚拟路径映射的实际物理目录,仅当imageUsingVirtualPath=yes时该配置有效

         imagePathFormat 上传保存路径,旧的是用时间戳生成文件名,我这边改成uuid文件后缀名,这样不容易重复,保证文件唯一性

 

 3 .修改ueditor-1.1.1.jar 的文件

         1.ConfigManager中getConfig方修改如下

 
       
 
       

       代码如下:

  public Map<String, Object> getConfig(int type)
  {
    Map conf = new HashMap();
    String savePath = null;
    boolean virtualPath = false;    //是否使用虚拟路径映射 默认不使用  
    switch (type)
    {
    case 4:
      conf.put("isBase64", "false");
      conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("fileMaxSize")));
      conf.put("allowFiles", getArray("fileAllowFiles"));
      conf.put("fieldName", this.jsonConfig.getString("fileFieldName"));
      savePath = this.jsonConfig.getString("filePathFormat");
      break;
    case 1:
      conf.put("isBase64", "false");
      conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("imageMaxSize")));
      conf.put("allowFiles", getArray("imageAllowFiles"));
      conf.put("fieldName", this.jsonConfig.getString("imageFieldName"));
      savePath = this.jsonConfig.getString("imagePathFormat");
      
      //for virtual path mapping   
      String imagePathFormat = this.jsonConfig.getString("imagePathFormat");  
      String imageUsingVirtualPath = this.jsonConfig.getString("imageUsingVirtualPath");  
      if("yes".equalsIgnoreCase(imageUsingVirtualPath)){  
          String imageRealMappingPath = this.jsonConfig.getString("imageRealMappingPath");  
          savePath = imageRealMappingPath + imagePathFormat;  
          virtualPath = true;  
          conf.put( "realMappingPath", imageRealMappingPath);//put into conf map using key=realMappingPath  
      } 
      break;
    case 3:
      conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("videoMaxSize")));
      conf.put("allowFiles", getArray("videoAllowFiles"));
      conf.put("fieldName", this.jsonConfig.getString("videoFieldName"));
      savePath = this.jsonConfig.getString("videoPathFormat");
      break;
    case 2:
      conf.put("filename", "scrawl");
      conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("scrawlMaxSize")));
      conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));
      conf.put("isBase64", "true");
      savePath = this.jsonConfig.getString("scrawlPathFormat");
      break;
    case 5:
      conf.put("filename", "remote");
      conf.put("filter", getArray("catcherLocalDomain"));
      conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("catcherMaxSize")));
      conf.put("allowFiles", getArray("catcherAllowFiles"));
      conf.put("fieldName", this.jsonConfig.getString("catcherFieldName") + "[]");
      savePath = this.jsonConfig.getString("catcherPathFormat");
      break;
    case 7:
      conf.put("allowFiles", getArray("imageManagerAllowFiles"));
      conf.put("dir", this.jsonConfig.getString("imageManagerListPath"));
      conf.put("count", Integer.valueOf(this.jsonConfig.getInt("imageManagerListSize")));
      break;
    case 6:
      conf.put("allowFiles", getArray("fileManagerAllowFiles"));
      conf.put("dir", this.jsonConfig.getString("fileManagerListPath"));
      conf.put("count", Integer.valueOf(this.jsonConfig.getInt("fileManagerListSize")));
    }

    conf.put("savePath", savePath);
    conf.put("rootPath", this.rootPath);
    conf.put( "virtualPath", virtualPath );//add put  
    return conf;
  }

         2.PathFormat文件修改如下:

        

     

      代码如下:

private static String getString(String pattern)
  {
    pattern = pattern.toLowerCase();

    if (pattern.indexOf("time") != -1)
      return getTimestamp();
    if (pattern.indexOf("yyyy") != -1)
      return getFullYear();
    if (pattern.indexOf("yy") != -1)
      return getYear();
    if (pattern.indexOf("mm") != -1)
      return getMonth();
    if (pattern.indexOf("dd") != -1)
      return getDay();
    if (pattern.indexOf("hh") != -1)
      return getHour();
    if (pattern.indexOf("ii") != -1)
      return getMinute();
    if (pattern.indexOf("ss") != -1)
      return getSecond();
    if (pattern.indexOf("rand") != -1) {
      return getRandom(pattern);
    }
    if (pattern.indexOf("uuid") != -1 ) {
        return generate();
    }

    return pattern;
  }

  public static String generate()
  {
      UUID uuid = UUID.randomUUID();
      return uuid.toString().replaceAll("-", "");
  }

          3.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, 5);
    }

    ServletFileUpload upload = new ServletFileUpload(
      new DiskFileItemFactory());

    if (isAjaxUpload) {
      upload.setHeaderEncoding("UTF-8");
    }
    try
    {
      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);
      }

      String savePath = (String)conf.get("savePath");
      String originFileName = fileStream.getName();
      String suffix = FileType.getSuffixByFilename(originFileName);

      originFileName = originFileName.substring(0, 
        originFileName.length() - suffix.length());
      savePath = savePath + suffix;

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

      if (!validType(suffix, (String[])conf.get("allowFiles"))) {
        return new BaseState(false, 8);
      }
      boolean virtualPath = (Boolean)conf.get("virtualPath");  
      savePath = PathFormat.parse(savePath, originFileName);

      String physicalPath = (String)conf.get("rootPath") + savePath;
      //启用虚拟路径时,不再使用 rootPath  
      if(virtualPath)  
      {  
        
          physicalPath = savePath; //此时savePath已含有实际的映射物理路径  
      }  
      InputStream is = fileStream.openStream();
      State storageState = StorageManager.saveFileByInputStream(is, 
        physicalPath, maxSize);
      is.close();

      if (storageState.isSuccess()) {
        storageState.putInfo("url", PathFormat.format(savePath));
      //启动虚拟路径时,返回ue的url将不再携带realMappingPath,否则ue无法正常加载对应的已上传的图片/视频等  
        if(virtualPath){  
            String temp = (String) conf.get("realMappingPath");  
            storageState.putInfo("url", PathFormat.format(savePath.substring(temp.length())));  
        }  
        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);
  }

 
 修改到这里就已经完毕了。

 有什么不明白的或者漏掉的可以留言

猜你喜欢

转载自xuzengfei.iteye.com/blog/2326557