/** * Gets the extension of a filename. * <p> * This method returns the textual part of the filename after the last dot. * There must be no directory separator after the dot. * <pre> * foo.txt --> "txt" * a/b/c.jpg --> "jpg" * a/b.txt/c --> "" * a/b/c --> "" * </pre> * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to retrieve the extension of. * @return the extension of the file or an empty string if none exists or {@code null} * if the filename is {@code null}. */ public static String getExtension(final String filename) { if (filename == null) { return null; } final int index = indexOfExtension(filename); if (index == NOT_FOUND) { return ""; } else { return filename.substring(index + 1); } }
/** * Checks whether the extension of the filename is that specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extension the extension to check for, null or empty checks for no extension * @return true if the filename has the specified extension * @throws java.lang.IllegalArgumentException if the supplied filename contains null bytes */ public static boolean isExtension(final String filename, final String extension) { if (filename == null) { return false; } failIfNullBytePresent(filename); if (extension == null || extension.isEmpty()) { return indexOfExtension(filename) == NOT_FOUND; } final String fileExt = getExtension(filename); return fileExt.equals(extension); }
/** * Checks whether the extension of the filename is one of those specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extensions the extensions to check for, null checks for no extension * @return true if the filename is one of the extensions * @throws java.lang.IllegalArgumentException if the supplied filename contains null bytes */ public static boolean isExtension(final String filename, final String[] extensions) { if (filename == null) { return false; } failIfNullBytePresent(filename); if (extensions == null || extensions.length == 0) { return indexOfExtension(filename) == NOT_FOUND; } final String fileExt = getExtension(filename); for (final String extension : extensions) { if (fileExt.equals(extension)) { return true; } } return false; }
/** * Checks whether the extension of the filename is one of those specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extensions the extensions to check for, null checks for no extension * @return true if the filename is one of the extensions * @throws java.lang.IllegalArgumentException if the supplied filename contains null bytes */ public static boolean isExtension(final String filename, final Collection<String> extensions) { if (filename == null) { return false; } failIfNullBytePresent(filename); if (extensions == null || extensions.isEmpty()) { return indexOfExtension(filename) == NOT_FOUND; } final String fileExt = getExtension(filename); for (final String extension : extensions) { if (fileExt.equals(extension)) { return true; } } return false; }
/** * Removes the extension from a filename. * <p> * This method returns the textual part of the filename before the last dot. * There must be no directory separator after the dot. * <pre> * foo.txt --> foo * a\b\c.jpg --> a\b\c * a\b\c --> a\b\c * a.b\c --> a.b\c * </pre> * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to query, null returns null * @return the filename minus the extension */ public static String removeExtension(final String filename) { if (filename == null) { return null; } failIfNullBytePresent(filename); final int index = indexOfExtension(filename); if (index == NOT_FOUND) { return filename; } else { return filename.substring(0, index); } }
/** * Removes the extension from a filename. * <p> * This method returns the textual part of the filename before the last dot. * There must be no directory separator after the dot. * <pre> * foo.txt --> foo * a\b\c.jpg --> a\b\c * a\b\c --> a\b\c * a.b\c --> a.b\c * </pre> * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to query, null returns null * @return the filename minus the extension */ public static String removeExtension(String filename) { if (filename == null) { return null; } int index = indexOfExtension(filename); if (index == -1) { return filename; } else { return filename.substring(0, index); } }
/** * Gets the extension of a filename. * <p> * This method returns the textual part of the filename after the last dot. * There must be no directory separator after the dot. * <pre> * foo.txt --> "txt" * a/b/c.jpg --> "jpg" * a/b.txt/c --> "" * a/b/c --> "" * </pre> * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to retrieve the extension of. * @return the extension of the file or an empty string if none exists. */ public static String getExtension(String filename) { if (filename == null) { return null; } int index = indexOfExtension(filename); if (index == -1) { return ""; } else { return filename.substring(index + 1); } }
/** * Checks whether the extension of the filename is that specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extension the extension to check for, null or empty checks for no extension * @return true if the filename has the specified extension */ public static boolean isExtension(String filename, String extension) { if (filename == null) { return false; } if (extension == null || extension.length() == 0) { return (indexOfExtension(filename) == -1); } String fileExt = getExtension(filename); return fileExt.equals(extension); }
/** * Checks whether the extension of the filename is one of those specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extensions the extensions to check for, null checks for no extension * @return true if the filename is one of the extensions */ public static boolean isExtension(String filename, String[] extensions) { if (filename == null) { return false; } if (extensions == null || extensions.length == 0) { return (indexOfExtension(filename) == -1); } String fileExt = getExtension(filename); for (int i = 0; i < extensions.length; i++) { if (fileExt.equals(extensions[i])) { return true; } } return false; }
/** * Checks whether the extension of the filename is one of those specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extensions the extensions to check for, null checks for no extension * @return true if the filename is one of the extensions */ public static boolean isExtension(String filename, Collection extensions) { if (filename == null) { return false; } if (extensions == null || extensions.isEmpty()) { return (indexOfExtension(filename) == -1); } String fileExt = getExtension(filename); for (Iterator it = extensions.iterator(); it.hasNext();) { if (fileExt.equals(it.next())) { return true; } } return false; }
@Test public void testIndexOfExtension() { assertEquals(-1, FilenameUtils.indexOfExtension(null)); assertEquals(-1, FilenameUtils.indexOfExtension("file")); assertEquals(4, FilenameUtils.indexOfExtension("file.txt")); assertEquals(13, FilenameUtils.indexOfExtension("a.txt/b.txt/c.txt")); assertEquals(-1, FilenameUtils.indexOfExtension("a/b/c")); assertEquals(-1, FilenameUtils.indexOfExtension("a\\b\\c")); assertEquals(-1, FilenameUtils.indexOfExtension("a/b.notextension/c")); assertEquals(-1, FilenameUtils.indexOfExtension("a\\b.notextension\\c")); }
/** * Returns the index of the last extension separator character, which is a dot. * <p/> * This method also checks that there is no directory separator after the last dot. To do this it uses * {@link #indexOfLastSeparator(String)} * <p/> * * @param filename * the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there is no such character */ public static int indexOfExtension( final String filename ) { return FilenameUtils.indexOfExtension( filename ); }
private boolean hasAllowedExtension(String fileName) { String extension = getExtension(fileName); for (String current : extensions) { if (equalsIgnoreCase(current, extension) && indexOfExtension(fileName) > 0) { return true; } } return false; } }
private boolean hasAllowedExtension(String fileName) { String extension = getExtension(fileName); for (String current : extensions) { if (equalsIgnoreCase(current, extension) && indexOfExtension(fileName) > 0) { return true; } } return false; } }
@Override public boolean isValid(File value, ConstraintValidatorContext context) { if (value != null && value.isFile()) { String extension = FilenameUtils.getExtension(value.getName()); return equalsIgnoreCase(expectedExtension, extension) && indexOfExtension(value.getName()) > 0; } return true; }
@Override public boolean isValid(File value, ConstraintValidatorContext context) { if (value != null && value.isFile()) { String extension = FilenameUtils.getExtension(value.getName()); return equalsIgnoreCase(expectedExtension, extension) && indexOfExtension(value.getName()) > 0; } return true; }
/** * Checks whether the extension of the filename is that specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extension the extension to check for, null or empty checks for no extension * @return true if the filename has the specified extension */ public static boolean isExtension(String filename, String extension) { if (filename == null) { return false; } if (extension == null || extension.length() == 0) { return (indexOfExtension(filename) == -1); } String fileExt = getExtension(filename); return fileExt.equals(extension); }
/** * Checks whether the extension of the filename is that specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extension the extension to check for, null or empty checks for no extension * @return true if the filename has the specified extension */ public static boolean isExtension(String filename, String extension) { if (filename == null) { return false; } if (extension == null || extension.length() == 0) { return (indexOfExtension(filename) == -1); } String fileExt = getExtension(filename); return fileExt.equals(extension); }
/** * Checks whether the extension of the filename is that specified. * <p> * This method obtains the extension as the textual part of the filename * after the last dot. There must be no directory separator after the dot. * The extension check is case-sensitive on all platforms. * * @param filename the filename to query, null returns false * @param extension the extension to check for, null or empty checks for no extension * @return true if the filename has the specified extension */ public static boolean isExtension(String filename, String extension) { if (filename == null) { return false; } if (extension == null || extension.length() == 0) { return indexOfExtension(filename) == -1; } String fileExt = getExtension(filename); return fileExt.equals(extension); }
/** * Get the stored (optionally versionned) name of a file name assuming it's in an associated unique folder. * * @param filename the name of the file to save. This will be URL encoded. * @param versionName the name of the version of the file. This will also be URL encoded. * @return a string representing the filename and version which is guaranteed not to collide with any other file * gotten through DefaultFilesystemStoreTools. * @since 11.0 */ public static String getStoredFilename(final String filename, final String versionName) { StringBuilder storedFileNameBuilder = new StringBuilder(FILE_NAME); if (versionName != null) { storedFileNameBuilder.append(FILE_VERSION_PREFIX); storedFileNameBuilder.append(versionName); } int extensionIndex = FilenameUtils.indexOfExtension(filename); if (extensionIndex != -1) { storedFileNameBuilder.append(filename.substring(extensionIndex)); } return storedFileNameBuilder.toString(); } }