Example usage for org.apache.commons.io FilenameUtils getExtension

List of usage examples for org.apache.commons.io FilenameUtils getExtension

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils getExtension.

Prototype

public static String getExtension(String filename) 

Source Link

Document

Gets the extension of a filename.

Usage

From source file:net.sf.jvifm.ResourceManager.java

public static Image getMimeImage(File file) {
    if (file.isDirectory())
        return folderImage;
    String extName = FilenameUtils.getExtension(file.getName());
    String path = mimeUtil.getMimeIconPath(extName);
    if (path == null)
        return fileImage;
    Image mimeImage = getImage(path);
    if (mimeImage == null)
        return fileImage;
    return mimeImage;
}

From source file:net.sourceforge.atunes.utils.TempFolder.java

@Override
public File addFile(final File srcFile, final String name) {
    File destFile = new File(StringUtils.getString(this.osManager.getTempFolder(),
            this.osManager.getFileSeparator(), name, ".", FilenameUtils.getExtension(srcFile.getName())));
    try {/*from   w  w w  .ja  v a 2  s  . co  m*/
        FileUtils.copyFile(srcFile, destFile);
    } catch (IOException e) {
        return null;
    }
    return destFile;
}

From source file:abfab3d.io.input.AttributedMeshReader.java

public AttributedMeshReader(String path) {
    m_path = path;//from   w w w.j a  va  2s. c o  m

    File f = new File(m_path);

    if (f.isDirectory()) {
        // got a directory, need to find the main file
        File[] files = f.listFiles();
        int len = files.length;
        String main = null;

        for (int i = 0; i < len; i++) {
            String ext = FilenameUtils.getExtension(files[i].getName());
            if (supportedExt.contains(ext)) {
                if (main != null) {
                    throw new IllegalArgumentException(
                            "Zip contains multiple main file.  First: " + main + " Second: " + files[i]);
                }

                main = files[i].getAbsolutePath();
            }
        }

        if (main == null) {
            throw new IllegalArgumentException("Zip does not contain a supported main file");
        }
        m_path = main;

        printf("main file: %s\n", m_path);
    }

    m_format = FilenameUtils.getExtension(m_path);
}

From source file:com.igormaznitsa.jcp.utils.PreprocessorUtils.java

@Nullable
public static String getFileExtension(@Nullable final File file) {
    String result = null;//  w w w.  ja  v  a  2 s. c om
    if (file != null) {
        result = FilenameUtils.getExtension(file.getName());
    }
    return result;
}

From source file:com.fibon.maven.confluence.ExportPageConfluenceMojo.java

public void execute() throws MojoFailureException {
    String extension = FilenameUtils.getExtension(outputFile.getPath());
    Format format = selectFormat(extension);
    if (format == null) {
        throw new MojoFailureException("Format " + format + " is not suported");
    } else {//from  w  ww. j a  v a2  s. c o  m
        Long pageId = getClient().getPageId(page);
        HttpGet request = prepareExportPageRequest(format, pageId);
        downloadFile(request);
    }
}

From source file:com.us.action.file.KindUpLoad.java

@Override
public String execute() throws Exception {
    if (gloabelError != null) {
        return text(getError(gloabelError.toString()));
    }//from   w  w w  .  j av a 2s. c  o  m

    if (imgFile == null) {
        return text(getError(getI118("${upload.nofile}", getRequest())));
    }

    String dirName = getRequest().getParameter("dir");
    if (dirName == null) {
        dirName = "image";
    }

    final String allowed = AppHelper.KIND_CONFIG.get(dirName).toString();
    String fileExt = FilenameUtils.getExtension(imgFileFileName);
    if (!Arrays.<String>asList(allowed.split(",")).contains(fileExt)) {
        return text(getError(getI118("${upload.invaildFile}", getRequest(), imgFileFileName, allowed)));
    }

    Object savePath = AppHelper.KIND_CONFIG.get("savePath");
    if (savePath == null) {
        return text(getError(getI118("${upload.noConfig}", getRequest(), "savePath")));
    }
    // 
    File uploadDir = FileHelper.getFile(AppHelper.APPSET.getFilePath(), savePath.toString());
    if (!uploadDir.exists())
        uploadDir.mkdirs();
    if (uploadDir == null || !uploadDir.isDirectory()) {
        return text(getError(getI118("${upload.dirNotExist}", getRequest())));
    }
    // ??
    if (!uploadDir.canWrite()) {
        return text(getError(getI118("${upload.dirCantWrite}", getRequest())));
    }

    Object type = AppHelper.KIND_CONFIG.get(dirName);
    if (type == null) {
        return text(getError(getI118("${upload.typeNoConfig}", getRequest(), "dirName")));
    }
    // ?URL
    String saveUrl = AppHelper.APPSET.getWebPath() + savePath.toString();

    if (!uploadDir.exists())
        uploadDir.mkdirs();

    String child = DateUtil.format(DateUtil.now(), "yyyyMM/dd/");
    uploadDir = FileHelper.getFile(uploadDir, child);
    saveUrl += child;
    if (!uploadDir.exists()) {
        uploadDir.mkdirs();
    }

    final long initSize = Long.parseLong(AppHelper.KIND_CONFIG.get("maxSize").toString());
    if (imgFile.length() > initSize * 1024000) {
        return text(getError(
                getI118("${upload.tooMax}", getRequest(), imgFileFileName, imgFile.length(), initSize)));
    }

    String newFileName = RandomHelper.uu_id() + "." + fileExt;
    File uploadedFile = new File(uploadDir, newFileName);
    final long startTime = System.currentTimeMillis();
    FileUtils.copyFile(imgFile, uploadedFile);
    log.info(String.format("?%s(ms)", (System.currentTimeMillis() - startTime)));

    return text(JSONHelper.obj2Json(KindResult
            .success(getI118("${upload.hasUpload}", getRequest(), imgFileFileName), saveUrl + newFileName)));
}

From source file:cz.zcu.pia.social.network.backend.services.FileService.java

public void resizeImage(File file) throws IOException, IllegalArgumentException {
    //Scale the image
    String ext = FilenameUtils.getExtension(file.getName());

    BufferedImage resizedImage = Scalr.resize(ImageIO.read(file), Constants.PROFILE_IMAGE_SIZE);
    ImageIO.write(resizedImage, ext, new File(Constants.BASE_PATH_RESIZED + file.getName()));
    File resizedFile = new File(Constants.BASE_PATH_RESIZED + file.getName());
    if (securityHelper.getLogedInUser().getUserImageName() != null) {
        new File(Constants.BASE_PATH_RESIZED + securityHelper.getLogedInUser().getUserImageName()).delete();
    }/*from  w  w  w .  jav a 2  s .c o  m*/

    securityHelper.getLogedInUser().setUserImageName(resizedFile.getName());
    usersService.update(securityHelper.getLogedInUser());
    file.delete();
}

From source file:io.wcm.devops.conga.generator.util.FileUtil.java

/**
 * Checks file extension/*from   www .j av  a  2  s  . c o m*/
 * @param file File to check
 * @param extension Expected file extension
 * @return true if file extension matches
 */
public static boolean matchesExtension(File file, String extension) {
    return matchesExtension(FilenameUtils.getExtension(file.getName()), extension);
}

From source file:foam.nanos.servlet.ImageServlet.java

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // get path/* ww  w.j a va 2  s .c  o  m*/
    String cwd = System.getProperty("user.dir");
    String[] paths = getServletConfig().getInitParameter("paths").split(":");
    String reqPath = req.getRequestURI().replaceFirst("/?images/?", "/");

    // enumerate each file path
    for (int i = 0; i < paths.length; i++) {
        File src = new File(cwd + "/" + paths[i] + reqPath);
        if (src.isFile() && src.canRead()
                && src.getCanonicalPath().startsWith(new File(paths[i]).getCanonicalPath())) {
            String ext = EXTS.get(FilenameUtils.getExtension(src.getName()));
            try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(src))) {
                resp.setContentType(!SafetyUtil.isEmpty(ext) ? ext : DEFAULT_EXT);
                resp.setHeader("Content-Disposition",
                        "filename=\"" + StringEscapeUtils.escapeHtml4(src.getName()) + "\"");
                resp.setContentLengthLong(src.length());

                IOUtils.copy(is, resp.getOutputStream());
                return;
            }
        }
    }

    resp.sendError(resp.SC_NOT_FOUND);
}

From source file:net.sf.jvifm.control.SystemCommand.java

public void execute() throws Exception {
    String ext = FilenameUtils.getExtension(cmd);

    if (runInshell) {
        Display.getDefault().syncExec(new Runnable() {
            public void run() {
                if (args != null & args.length > 0) {
                    try {
                        Runtime.getRuntime().exec(cmdArray, null, new File(pwd));
                    } catch (Exception e) {

                    }//from  w w w .ja  va2  s.  c o  m
                } else {
                    Program.launch(cmd, pwd);
                }
            }
        });
        return;
    }
    Program program = Program.findProgram(ext);
    if (!isFileShortcut || cmdArray.length > 1) {
        if (ext.equals("bat") || ext.equals("sh")) {
            Runtime.getRuntime().exec(cmdArray, null, new File(cmd).getParentFile());
        } else {
            Runtime.getRuntime().exec(cmdArray, null, new File(pwd));
        }
    } else {
        if (ext.equals("bat") || ext.equals("sh") || program == null) {
            Runtime.getRuntime().exec(new String[] { cmd }, null, new File(cmd).getParentFile());
        } else {
            Display.getDefault().syncExec(new Runnable() {
                public void run() {
                    Program.launch(cmd, pwd);
                }
            });
        }
    }

}