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:eu.novait.imageresizer.helpers.ImageConverter.java

public void process() throws IOException {
    BufferedImage inImage = ImageIO.read(this.file);
    double ratio = (double) inImage.getWidth() / (double) inImage.getHeight();
    int w = this.width;
    int h = this.height;
    if (inImage.getWidth() >= inImage.getHeight()) {
        w = this.width;
        h = (int) Math.round(w / ratio);
    } else {//from   w w  w  . j av  a2  s  .c o m
        h = this.height;
        w = (int) Math.round(ratio * h);
    }
    int type = inImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : inImage.getType();
    BufferedImage outImage = new BufferedImage(w, h, type);
    Graphics2D g = outImage.createGraphics();
    g.drawImage(inImage, 0, 0, w, h, null);
    g.dispose();
    String ext = FilenameUtils.getExtension(this.file.getAbsolutePath());
    String t = "jpg";
    switch (ext) {
    case "png":
        t = "png";
        break;
    }
    ImageIO.write(outImage, t, this.outputfile);
}

From source file:eu.edisonproject.utility.commons.IDFSort.java

@Override
public Map<String, Double> sort(Map<String, Double> termDictionaray, String dirPath)
        throws IOException, InterruptedException {
    Map<String, Double> newTermDictionaray = new HashMap<>();
    File dir = new File(dirPath);
    File[] docs = dir.listFiles();
    //        GrepOptionSets go = new GrepOptionSets();
    for (String term : termDictionaray.keySet()) {
        int numOfDocsWithTerm = 0;
        for (File f : docs) {
            int count = 0;
            if (FilenameUtils.getExtension(f.getName()).endsWith("txt")) {
                count++;// w w w .j a v a2 s .  c  om
                Logger.getLogger(IDFSort.class.getName()).log(Level.FINE, "{0}: {1} of {2}",
                        new Object[] { f.getName(), count, docs.length });
                ReaderFile rf = new ReaderFile(f.getAbsolutePath());
                String contents = rf.readFileWithN();
                cleanStopWord.setDescription(contents);
                contents = cleanStopWord.execute();

                cleanStopWord.setDescription(term.replaceAll("_", " "));
                String cTerm = cleanStopWord.execute();

                //                    int lastIndex = 0;
                //                    int wcount = 0;
                //
                //                    while (lastIndex != -1) {
                //
                //                        lastIndex = contents.indexOf(cTerm, lastIndex);
                //
                //                        if (lastIndex != -1) {
                //                            wcount++;
                //                            lastIndex += cTerm.length();
                //                        }
                //                    }
                //                    System.out.println(f.getName() + "," + cTerm + "," + wcount);
                //
                //                    wcount = StringUtils.countMatches(contents, cTerm);
                //                    System.out.println(f.getName() + "," + cTerm + "," + wcount);
                if (contents.contains(cTerm)) {
                    numOfDocsWithTerm++;
                    //                        System.out.println(f.getName() + "," + cTerm + "," + numOfDocsWithTerm);
                }
                //                    try (InputStream fis = new FileInputStream(f)) {
                //                        String result = Unix4j.from(fis).grep(go.i.count, cTerm).toStringResult();
                //                        Integer lineCount = Integer.valueOf(result);
                //                        if (lineCount > 0) {
                //                            numOfDocsWithTerm++;
                //                        }
                //                    }
            }

        }
        double idf = 0;
        if (numOfDocsWithTerm > 0) {
            idf = Math.log((double) docs.length / (double) numOfDocsWithTerm);
        }
        newTermDictionaray.put(term, idf);
    }
    return newTermDictionaray;
}

From source file:net.orpiske.ssps.common.groovy.GroovyClasspathWalker.java

@Override
protected void handleFile(File file, int depth, Collection results) throws IOException

{
    String ext = FilenameUtils.getExtension(file.getName());

    if ((".class").equals(ext) || (".jar").equals(ext) || (".zip").equals(ext)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Loading to the classpath: " + file.getAbsolutePath());
        }//w w  w  .  jav  a 2s.  c  om

        loader.addClasspath(file.getCanonicalPath());
    }
}

From source file:com.jaeksoft.searchlib.streamlimiter.StreamLimiterInputStream.java

@Override
public File getFile() throws IOException {
    String ext = originalFileName == null ? null : FilenameUtils.getExtension(originalFileName);
    return getTempFile(ext);
}

From source file:it.attocchi.utils.HttpClientUtils.java

/**
 * /*from  w ww  .  j ava  2 s .c om*/
 * @param url
 *            un url dove scaricare il file
 * @param urlFileNameParam
 *            specifica il parametro nell'url che definisce il nome del
 *            file, se non specificato il nome corrisponde al nome nell'url
 * @param dest
 *            dove salvare il file, se non specificato crea un file
 *            temporaneo
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
public static File getFile(String url, String urlFileNameParam, File dest)
        throws ClientProtocolException, IOException {

    DefaultHttpClient httpclient = null;
    HttpGet httpget = null;
    HttpResponse response = null;
    HttpEntity entity = null;

    try {
        httpclient = new DefaultHttpClient();

        url = url.trim();

        /* Per prima cosa creiamo il File */
        String name = getFileNameFromUrl(url, urlFileNameParam);
        String baseName = FilenameUtils.getBaseName(name);
        String extension = FilenameUtils.getExtension(name);

        if (dest == null)
            dest = File.createTempFile(baseName + "_", "." + extension);

        /* Procediamo con il Download */
        httpget = new HttpGet(url);
        response = httpclient.execute(httpget);
        entity = response.getEntity();

        if (entity != null) {

            OutputStream fos = null;
            try {
                // var fos = new
                // java.io.FileOutputStream('c:\\temp\\myfile.ext');
                fos = new java.io.FileOutputStream(dest);
                entity.writeTo(fos);
            } finally {
                if (fos != null)
                    fos.close();
            }

            logger.debug(fos);
        }
    } catch (Exception ex) {
        logger.error(ex);
    } finally {
        // org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpClient);
        // if (entity != null)
        // EntityUtils.consume(entity);
        //

        try {
            if (httpclient != null)
                httpclient.getConnectionManager().shutdown();
        } catch (Exception ex) {
            logger.error(ex);
        }
    }

    return dest;
}

From source file:com.streamsets.pipeline.stage.origin.spooldir.TestOffsetUtil.java

private static Map<String, String> removeAbsolutePaths(Map<String, String> offsetV2) {
    Map<String, String> resultMap = new HashMap<>();
    for (Map.Entry<String, String> entry : offsetV2.entrySet()) {
        if (entry.getKey().contains(PATH_SEPARATOR)) {
            String filename = new StringJoiner(".").add(FilenameUtils.getBaseName(entry.getKey()))
                    .add(FilenameUtils.getExtension(entry.getKey())).toString();
            resultMap.put(filename, entry.getValue());
        } else {/*from ww w .j av  a2 s .  c o  m*/
            resultMap.put(entry.getKey(), entry.getValue());
        }
    }
    return resultMap;
}

From source file:com.intuit.tank.script.TestUploadBean.java

/**
 * Saves the script in the database.// www .j a  va2  s  .c  o m
 */
public String save() {
    UploadedFile item = getFile();
    try {
        String fileName = item.getFileName();
        fileName = FilenameUtils.getBaseName(fileName) + "-" + UUID.randomUUID().toString() + "."
                + FilenameUtils.getExtension(fileName);
        File parent = new File("uploads");
        parent.mkdirs();
        File f = new File(parent, fileName);
        LOG.info("Writing file to " + f.getAbsolutePath());
        FileUtils.writeByteArrayToFile(f, item.getContents());
        messages.info("Wrote file to " + f.getAbsolutePath());
    } catch (Exception e) {
        messages.error(e.getMessage());
    }
    return null;
}

From source file:io.github.sn0cr.rapidRunner.testRunner.TestCaseFinder.java

public TestCaseFinder(Path parentFolder, String filePattern, String inExtension, String outExtension) {
    final String[] files = parentFolder.toFile().list(new WildcardFileFilter(filePattern));
    Arrays.sort(files, new NaturalOrderComparator());
    for (final String filename : files) {
        final String extension = FilenameUtils.getExtension(filename);
        final String name = FilenameUtils.getBaseName(filename);
        final Path toFile = Paths.get(parentFolder.toString(), filename);
        Pair<Path, Path> testCasePair;
        if (this.testCases.containsKey(name)) {
            testCasePair = this.testCases.get(name);
        } else {//from w  w  w . j av a  2 s .  c om
            testCasePair = new Pair<Path, Path>();
        }
        if (extension.equals(inExtension)) {
            testCasePair.setA(toFile);
        } else if (extension.equals(outExtension)) {
            testCasePair.setB(toFile);
        }
        if (testCasePair.notEmpty()) {
            this.testCases.put(name, testCasePair);
        }
    }
}

From source file:eu.novait.imagerenamer.model.ImageFile.java

public ImageFile(String filepath) {
    this.filepath = new File(filepath);
    this.filename = this.filepath.getName();
    this.proposedFilename = "";
    this.extension = FilenameUtils.getExtension(this.filepath.getName());
    try {/*from  w  w w . j  a v  a  2 s .com*/
        BasicFileAttributes attr = Files.readAttributes(this.filepath.toPath(), BasicFileAttributes.class);
        this.fileCreationDate = new Date(attr.creationTime().toMillis());
    } catch (IOException ex) {
        Logger.getLogger(ImageFile.class.getName()).log(Level.SEVERE, null, ex);
    }
    createThumbnail();
}

From source file:brut.directory.ZipUtils.java

private static void processFolder(final File folder, final ZipOutputStream zipOutputStream,
        final int prefixLength) throws BrutException, IOException {
    for (final File file : folder.listFiles()) {
        if (file.isFile()) {
            final String cleanedPath = BrutIO.sanitizeUnknownFile(folder,
                    file.getPath().substring(prefixLength));
            final ZipEntry zipEntry = new ZipEntry(BrutIO.normalizePath(cleanedPath));

            // aapt binary by default takes in parameters via -0 arsc to list extensions that shouldn't be
            // compressed. We will replicate that behavior
            final String extension = FilenameUtils.getExtension(file.getAbsolutePath());
            if (mDoNotCompress != null
                    && (mDoNotCompress.contains(extension) || mDoNotCompress.contains(zipEntry.getName()))) {
                zipEntry.setMethod(ZipEntry.STORED);
                zipEntry.setSize(file.length());
                BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(file));
                CRC32 crc = BrutIO.calculateCrc(unknownFile);
                zipEntry.setCrc(crc.getValue());
                unknownFile.close();//w w  w.ja  va  2s. com
            } else {
                zipEntry.setMethod(ZipEntry.DEFLATED);
            }

            zipOutputStream.putNextEntry(zipEntry);
            try (FileInputStream inputStream = new FileInputStream(file)) {
                IOUtils.copy(inputStream, zipOutputStream);
            }
            zipOutputStream.closeEntry();
        } else if (file.isDirectory()) {
            processFolder(file, zipOutputStream, prefixLength);
        }
    }
}