Java tutorial
/* * Copyright (c) Sergiu Giurgiu 2011. * * This file is part of TVMan. * * TVMan is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TVMan is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TVMan. If not, see <http://www.gnu.org/licenses/>. */ package com.zergiu.tvman.controllers; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; import java.util.TreeMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * @author sergiu * */ @Controller @RequestMapping("/browseFileSystem") public class FSBrowseController { private static Logger log = LoggerFactory.getLogger(FSBrowseController.class); @RequestMapping(value = "/children", method = RequestMethod.POST) public @ResponseBody Map<String, String> getChildren( @RequestParam(value = "parentPath", required = false) String parentPath) { Map<String, String> children = new TreeMap<String, String>(); if (parentPath == null || "".equals(parentPath)) { addRootPaths(children); } else { try { Path parent = FileSystems.getDefault().getPath(parentPath); addParentFolder(children, parent); addChildFolders(children, parent); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage(), e); } } log.debug("got files " + children); return children; } /** * @param children */ private void addRootPaths(Map<String, String> children) { log.debug("reading root files "); Iterable<Path> roots = FileSystems.getDefault().getRootDirectories(); for (Path f : roots) { children.put(f.toString(), f.toString()); } } /** * @param children * @param parent */ private void addParentFolder(Map<String, String> children, Path parent) { Path parentOfParent = parent.getParent(); if (parentOfParent == null) { children.put("", ".."); } else { children.put(parentOfParent.toString(), ".."); } } /** * @param children * @param parent * @return * @throws IOException */ private void addChildFolders(Map<String, String> children, Path parent) throws IOException { try (DirectoryStream<Path> paths = Files.newDirectoryStream(parent)) { for (Path path : paths) { if (Files.isDirectory(path)) { Path name = path.getFileName(); children.put(path.toString(), name.toString()); } } } //try } }