@RequestMapping("/create/directory")
  public String createDirectory(
      @RequestParam(value = "parentPath") String parentPath,
      @RequestParam(value = "name") String name,
      RedirectAttributes redirectAttributes)
      throws IOException {

    // 删除最后的/
    name = FilenameUtils.normalizeNoEndSeparator(name);

    if (isValidFileName(name)) {
      String rootPath = sc.getRealPath(ROOT_DIR);
      parentPath = URLDecoder.decode(parentPath, Constants.ENCODING);

      File parent = new File(rootPath + File.separator + parentPath);
      File currentDirectory = new File(parent, name);
      boolean result = currentDirectory.mkdirs();
      if (result == false) {
        redirectAttributes.addFlashAttribute(Constants.ERROR, "名称为[" + name + "]的文件/目录已经存在");
      } else {
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "创建成功!");
      }
    } else {
      redirectAttributes.addFlashAttribute(Constants.ERROR, "名称为[" + name + "]不是合法的文件名,请重新命名");
    }

    redirectAttributes.addAttribute("path", parentPath);
    return redirectToUrl(viewName("list"));
  }
Example #2
0
 /**
  * Checks and normalizes the given path
  *
  * @param path The path to clean up
  * @return a normalized version of the path, with single separators between path components and
  *     dot components resolved
  */
 public static String cleanPath(String path) throws InvalidPathException {
   validatePath(path);
   return FilenameUtils.separatorsToUnix(FilenameUtils.normalizeNoEndSeparator(path));
 }
Example #3
0
 /**
  * Join two path without appending '/'.
  *
  * @param path1 path1
  * @param path2 path2
  * @return joined path
  */
 public static String join(String path1, String path2) {
   path1 = trimSeparator(path1);
   path2 = trimSeparator(path2);
   return FilenameUtils.normalizeNoEndSeparator(path1 + "/" + path2, true);
 }
Example #4
0
  /**
   * Get the relative path from one file to another, specifying the directory separator. If one of
   * the provided resources does not exist, it is assumed to be a file unless it ends with '/' or
   * '\'.
   *
   * @param target targetPath is calculated to this file
   * @param base basePath is calculated from this file
   * @param separator directory separator. The platform default is not assumed so that we can test
   *     Unix behaviour when running on Windows (for example)
   * @return
   */
  public static String relativize(
      final String targetPath, final String basePath, final String fileSeparator)
      throws PathRelativizationException {

    // Normalize the paths
    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
    String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);

    // Undo the changes to the separators made by normalization
    if (fileSeparator.equals("/")) {
      normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath);
      normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath);

    } else if (fileSeparator.equals("\\")) {
      normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath);
      normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath);
    } else {
      throw new IllegalArgumentException("Unrecognised dir separator '" + fileSeparator + "'");
    }

    String[] base = normalizedBasePath.split(Pattern.quote(fileSeparator));
    String[] target = normalizedTargetPath.split(Pattern.quote(fileSeparator));

    // First get all the common elements. Store them as a string,
    // and also count how many of them there are.
    StringBuffer common = new StringBuffer();

    int commonIndex = 0;
    while (commonIndex < target.length
        && commonIndex < base.length
        && target[commonIndex].equals(base[commonIndex])) {
      common.append(target[commonIndex] + fileSeparator);
      commonIndex++;
    }

    if (commonIndex == 0) {
      // No single common path element. This most
      // likely indicates differing drive letters, like C: and D:.
      // These paths cannot be relativized.
      throw new PathRelativizationException(
          "No common path element found for '"
              + normalizedTargetPath
              + "' and '"
              + normalizedBasePath
              + "'");
    }

    // The number of directories we have to backtrack depends on whether the base is a file or a dir
    // For example, the relative path from
    //
    // /foo/bar/baz/gg/ff to /foo/bar/baz
    //
    // ".." if ff is a file
    // "../.." if ff is a directory
    //
    // The following is a heuristic to figure out if the base refers to a file or dir. It's not
    // perfect, because
    // the resource referred to by this path may not actually exist, but it's the best I can do
    boolean baseIsFile = true;

    File baseResource = new File(normalizedBasePath);

    if (baseResource.exists()) {
      baseIsFile = baseResource.isFile();

    } else if (basePath.endsWith(fileSeparator)) {
      baseIsFile = false;
    }

    StringBuffer relative = new StringBuffer();

    if (base.length != commonIndex) {
      int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;

      for (int i = 0; i < numDirsUp; i++) {
        relative.append(".." + fileSeparator);
      }
    }
    relative.append(normalizedTargetPath.substring(common.length()));
    return relative.toString();
  }