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:com.isomorphic.maven.packaging.Module.java

/**
 * Convenience method for attaching {@link SubArtifact SubArtifacts} to this Artifact.
 * @param file//from   w  w  w  . j av  a2s.com
 * @param classifier
 */
public void attach(File file, String classifier) {
    subs.add(new SubArtifact(this, classifier, FilenameUtils.getExtension(file.getName()), file));
}

From source file:com.eufar.emc.server.UploadFunction.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("UploadFunction - the function started");
    response.setContentType("text/html;charset=UTF-8");
    response.addHeader("Cache-Control", "no-cache,no-store");
    @SuppressWarnings("unused")
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    try {/*  ww  w.java  2s  .  co m*/
        List<?> items = upload.parseRequest(request);
        Iterator<?> iter = items.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj == null) {
                continue;
            }
            org.apache.commons.fileupload.FileItem item = (org.apache.commons.fileupload.FileItem) obj;
            if (FilenameUtils.getExtension(item.getName()).matches("(xml|XML)")) {
                if (item.isFormField()) {
                    String name = item.getName();
                    String value = "";
                    if (name.compareTo("textBoxFormElement") == 0) {
                        value = item.getString();
                    } else {
                        value = item.getString();
                    }
                    response.getWriter().write(name + "=" + value + "\n");
                } else {
                    byte[] fileContents = item.get();
                    String message = new String(fileContents);
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("text/html");
                    response.getWriter().write(message);
                    System.out.println("UploadFunction - file uploaded");
                }
            } else {
                System.out.println("UploadFunction - file rejected: wrong format");
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html");
                response.getWriter().write("format");
            }
        }
    } catch (Exception ex) {
        System.out.println("UploadFunction - a problem occured: " + ex);
        response.getWriter().write("ERROR:" + ex.getMessage());
    }
}

From source file:ffx.potential.parsers.KeyFileFilter.java

/**
 * {@inheritDoc}// w  ww.j  ava 2s.  co  m
 *
 * This method return <code>true</code> if the file is a directory or Force
 * Field X script (*.FFX).
 */
@Override
public boolean accept(File file) {
    if (file.isDirectory()) {
        return true;
    }
    String ext = FilenameUtils.getExtension(file.getName()).toUpperCase();

    return ext.startsWith("KEY") || ext.startsWith("PRM") || ext.startsWith("PROPERTIES");
}

From source file:ijfx.core.image.sampler.DatasetSamplerService.java

/**
 * Isolate a point of a dimension of a dataset. It means for instance that
 * if you have a dataset with CHANNEL and Z, you can isolate a the Z stack
 * corresponding to a single channel just by doing :
 * sampleService.isolateDimension(myDataset,Axes.CHANNEL,2)
 *
 * @param dataset the input dataset//from  ww w .j a  v a2 s.  c o  m
 * @param axes the axe type to isolate
 * @param position the position in the dimension
 * @return
 */
public Dataset isolateDimension(Dataset dataset, AxisType axes, long position) {

    SamplingDefinition def = new SamplingDefinition(dataset);
    AxisSubrange subrange = new AxisSubrange(position);

    def.constrain(axes, subrange);

    Dataset output = createOutputImage(dataset, def);

    copyData(def, dataset, output);
    String name = output.getName();
    name = FilenameUtils.getBaseName(name);
    String extension = FilenameUtils.getExtension(output.getName());
    output.setName(String.format("%s - %s %d.%s", name, axes.getLabel(), position, extension));
    return output;

}

From source file:net.pms.dlna.RarredEntry.java

@Override
public String getSystemName() {
    return FilenameUtils.getBaseName(file.getAbsolutePath()) + "." + FilenameUtils.getExtension(name);
}

From source file:esiptestbed.mudrod.ontology.pre.AggregateTriples.java

/**
 * Method of executing triple aggregation
 *//*  ww  w . ja v  a 2s .c o  m*/
@Override
public Object execute() {
    File file = new File(this.props.getProperty("oceanTriples"));
    if (file.exists()) {
        file.delete();
    }
    try {
        file.createNewFile();
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    FileWriter fw;
    try {
        fw = new FileWriter(file.getAbsoluteFile());
        bw = new BufferedWriter(fw);
    } catch (IOException e) {
        e.printStackTrace();
    }

    File[] files = new File(this.props.getProperty("ontologyInputDir")).listFiles();
    for (File file_in : files) {
        String ext = FilenameUtils.getExtension(file_in.getAbsolutePath());
        if ("owl".equals(ext)) {
            try {
                loadxml(file_in.getAbsolutePath());
                getAllClass();
            } catch (JDOMException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    }

    try {
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.discovery.darchrow.io.MimeTypeUtil.java

/**
 *  content type by file name.<br>
 * /*from w w  w  .  ja  v a2 s . co m*/
 * //TODO
 * <b>
 * Very incomplete function. As of Java 7, html, pdf and jpeg extensions return the correct mime-type but js and css return null! </b> <br>
 * 
 * <p>
 * I tried Apache Tika but it is huge with tons of dependencies, <br>
 * URLConnection doesn't use the bytes of the files, <br>
 * MimetypesFileTypeMap also just looks at files names,<br>
 * and I couldn't move to Java 7.
 * </p>
 * 
 * @param fileName
 *            the file name
 * @return the content type by file name
 * @see java.net.URLConnection#getFileNameMap()
 * @see javax.activation.MimetypesFileTypeMap#getContentType(java.io.File)
 * @see java.net.FileNameMap#getContentTypeFor(String)
 * @see org.apache.commons.io.FilenameUtils#getExtension(String)
 * @see java.net.URLConnection#guessContentTypeFromName(String)
 * @see java.net.URLConnection#guessContentTypeFromStream(java.io.InputStream)
 * @see "java.nio.file.Files#probeContentType(java.nio.file.Path)"
 */
public static String getContentTypeByFileName(String fileName) {

    String extension = FilenameUtils.getExtension(fileName);
    if (Validator.isNullOrEmpty(extension)) {
        return null;
    }

    // 1. first use java's build-in utils
    //??? mimetable? "content.types.user.table" ?? java  lib/content-types.properties 
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentType = fileNameMap.getContentTypeFor(fileName);

    // 2. nothing found -> lookup our in extension map to find types like ".doc" or ".docx"
    if (Validator.isNullOrEmpty(contentType)) {
        contentType = fileExtensionMap.get(extension.toLowerCase());
    }

    return contentType;
}

From source file:eu.edisonproject.classification.prepare.controller.Text2Avro.java

@Override
public void execute() {
    File file = new File(inputFolder);
    Document davro;/*from  w  ww  .j  a  v a 2 s.c o  m*/
    DocumentAvroSerializer dAvroSerializer = null;
    if (file.isDirectory()) {
        File[] filesInDir = file.listFiles();
        Arrays.sort(filesInDir);
        for (File f : filesInDir) {
            if (FilenameUtils.getExtension(f.getName()).endsWith("txt")) {
                Path p = Paths.get(f.getAbsolutePath());
                BasicFileAttributes attr = null;
                try {
                    attr = Files.readAttributes(p, BasicFileAttributes.class);
                } catch (IOException ex) {
                    Logger.getLogger(Text2Avro.class.getName()).log(Level.SEVERE, null, ex);
                }
                FileTime date = attr.creationTime();

                //                    DateTimeFormatter formatter
                //                            = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
                //                    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

                //                    System.err.println(LocalDate.parse(date.toString(), formatter));
                documentObject = new DocumentObject();
                extract(this.getDocumentObject(), f.getPath());
                documentObject.setDescription(documentObject.getDescription().toLowerCase());
                clean(this.getDocumentObject().getDescription());
                if (documentObject.getDescription().equals("")) {
                    continue;
                }
                //                    documentObject.setDate(LocalDate.parse(date.toString(), formatter));
                documentObjectList.add(this.getDocumentObject());

                davro = new Document();
                davro.setDocumentId(documentObject.getDocumentId());
                davro.setTitle(documentObject.getTitle());
                davro.setDate(documentObject.getDate().toString());
                davro.setDescription(documentObject.getDescription());

                if (dAvroSerializer == null) {
                    dAvroSerializer = new DocumentAvroSerializer(
                            outputFolder + File.separator + documentObject.getTitle() + date + ".avro",
                            davro.getSchema());
                }
                dAvroSerializer.serialize(davro);
            }
        }
        if (dAvroSerializer != null) {
            dAvroSerializer.close();
            dAvroSerializer = null;
        }

    } else {
        System.out.println("NOT A DIRECTORY");
    }
}

From source file:ffx.potential.parsers.InducedFileFilter.java

/**
 * {@inheritDoc}/*w  ww .  j  a va  2 s . c om*/
 *
 * This method return <code>true</code> if the file is a directory or
 * matches the extension for TINKER induced dipole (*.*U) file.
 */
@Override
public boolean accept(File file) {
    if (file.isDirectory()) {
        return true;
    }
    String ext = FilenameUtils.getExtension(file.getName());
    return ext.toUpperCase().endsWith("U");
}

From source file:de.sanandrew.mods.turretmod.registry.assembly.TurretAssemblyRecipes.java

private static boolean processJson(Path file,
        Ex2Function<JsonObject, Boolean, JsonParseException, IOException> callback) {
    if (!"json".equals(FilenameUtils.getExtension(file.toString()))
            || FilenameUtils.getName(file.toString()).startsWith("_")) {
        return true;
    }/* ww w. jav  a 2  s. c o m*/

    try (BufferedReader reader = Files.newBufferedReader(file)) {
        JsonObject json = JsonUtils.fromJson(reader, JsonObject.class);

        if (json == null || json.isJsonNull()) {
            throw new JsonSyntaxException("Json cannot be null");
        }

        callback.apply(json);

        return true;
    } catch (JsonParseException e) {
        TmrConstants.LOG.log(Level.ERROR,
                String.format("Parsing error loading assembly table recipe from %s", file), e);
        return false;
    } catch (IOException e) {
        TmrConstants.LOG.log(Level.ERROR, String.format("Couldn't read recipe from %s", file), e);
        return false;
    }
}