public int compare(File file1, File file2) { String ext1 = FileNameUtil.getExtension(file1.getName()); String ext2 = FileNameUtil.getExtension(file2.getName()); long diff = ext1.compareToIgnoreCase(ext2); if (diff == 0) { return 0; } if (diff > 0) { return order; } return -order; } }
/** * Prepares response for various provided data. * * @param response http response * @param fileName file name, if full path then file name will be stripped, if null, will be ignored. * @param mimeType mime type with optional charset, may be <code>null</code> * @param fileSize if less then 0 it will be ignored */ public static void prepareResponse(final HttpServletResponse response, final String fileName, String mimeType, final int fileSize) { if ((mimeType == null) && (fileName != null)) { String extension = FileNameUtil.getExtension(fileName); mimeType = MimeTypes.getMimeType(extension); } if (mimeType != null) { response.setContentType(mimeType); } if (fileSize >= 0) { response.setContentLength(fileSize); } // support internationalization // See https://tools.ietf.org/html/rfc6266#section-5 for more information. if (fileName != null) { String name = FileNameUtil.getName(fileName); String encodedFileName = URLCoder.encode(name); response.setHeader(CONTENT_DISPOSITION, "attachment;filename=\"" + name + "\";filename*=utf8''" + encodedFileName); } }
public static String doConcat(String basePath, String fullFilenameToAdd, char separator) { int prefix = getPrefixLength(fullFilenameToAdd); if (prefix < 0) { return null; } if (prefix > 0) { return doNormalize(fullFilenameToAdd, separator, true); } if (basePath == null) { return null; } int len = basePath.length(); if (len == 0) { return doNormalize(fullFilenameToAdd, separator, true); } char ch = basePath.charAt(len - 1); if (isSeparator(ch)) { return doNormalize(basePath + fullFilenameToAdd, separator, true); } else { return doNormalize(basePath + '/' + fullFilenameToAdd, separator, true); } }
/** * Converts all separators to the system separator. * * @param path the path to be changed, null ignored * @return the updated path */ public static String separatorsToSystem(String path) { if (path == null) { return null; } if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) { return separatorsToWindows(path); } else { return separatorsToUnix(path); } }
/** * Gets the base name, minus the full path and extension, from a full filename. * <p> * This method will handle a file in either Unix or Windows format. * The text after the last forward or backslash and before the last dot is returned. * <pre>{@code * a/b/c.txt --> c * a.txt --> a * a/b/c --> 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 name of the file without the path, or an empty string if none exists */ public static String getBaseName(String filename) { return removeExtension(getName(filename)); }
@Test void testExtension() { assertEquals("foo", FileNameUtil.getExtension("/a/b/c.foo")); assertEquals("doo", FileNameUtil.getExtension("/a/b/c.foo.doo")); assertEquals("", FileNameUtil.getExtension("/a/b/c")); assertTrue(FileNameUtil.hasExtension("/a/b/c.foo")); assertTrue(FileNameUtil.hasExtension("/a/b/c.foo.doo")); assertFalse(FileNameUtil.hasExtension("/a/b/c")); }
return null; int prefix = getPrefixLength(filename); if (prefix < 0) { return null; return getPrefix(filename); // add end slash if necessary } else { return filename; int index = indexOfLastSeparator(filename); if (index < 0) { return filename.substring(0, prefix);
public FileUploadable(final File file) { this.file = file; this.fileName = FileNameUtil.getName(file.getName()); this.mimeType = null; }
/** * Returns <code>true</code> if some JAR file has to be accepted. */ protected boolean acceptJar(File jarFile) { String path = jarFile.getAbsolutePath(); path = FileNameUtil.separatorsToUnix(path); return rulesJars.match(path); }
/** * Does the work of getting the path. * * @param filename the filename * @param separatorAdd 0 to omit the end separator, 1 to return it * @return the path */ private static String doGetPath(String filename, int separatorAdd) { if (filename == null) { return null; } int prefix = getPrefixLength(filename); if (prefix < 0) { return null; } int index = indexOfLastSeparator(filename); int endIndex = index + separatorAdd; if (prefix >= filename.length() || index < 0 || prefix >= endIndex) { return StringPool.EMPTY; } return filename.substring(prefix, endIndex); }
} else { localFile += '/' + FileNameUtil.getPathNoEndSeparator(actionPath) + '/' + src; localUrl += contextPath + src; } else { localUrl += contextPath + FileNameUtil.getPath(actionPath) + '/' + src;
return null; int len = getPrefixLength(filename); if (len < 0) { return null;
public static String concat(String basePath, String fullFilenameToAdd, boolean unixSeparator) { char separator = (unixSeparator ? UNIX_SEPARATOR : WINDOWS_SEPARATOR); return doConcat(basePath, fullFilenameToAdd, separator); } public static String doConcat(String basePath, String fullFilenameToAdd, char separator) {
@Test void testGetPathNoEndSeparator() { assertEquals("", FileNameUtil.getPathNoEndSeparator("/hello.world.html")); assertEquals("foo", FileNameUtil.getPathNoEndSeparator("/foo/hello.world.html")); assertEquals("foo/bar", FileNameUtil.getPathNoEndSeparator("/foo/bar/hello.world.html")); }
/** * Gets the base name, minus the full path and extension, from a full filename. * <p> * This method will handle a file in either Unix or Windows format. * The text after the last forward or backslash and before the last dot is returned. * <pre>{@code * a/b/c.txt --> c * a.txt --> a * a/b/c --> 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 name of the file without the path, or an empty string if none exists */ public static String getBaseName(final String filename) { return removeExtension(getName(filename)); }
return null; int prefix = getPrefixLength(filename); if (prefix < 0) { return null; return getPrefix(filename); // add end slash if necessary } else { return filename; int index = indexOfLastSeparator(filename); if (index < 0) { return filename.substring(0, prefix);
/** * Set content ID if it is missing. * * @return this * @see #contentId(String) */ protected EmailAttachmentBuilder setContentIdFromNameIfMissing() { if (contentId == null) { if (name != null) { contentId(FileNameUtil.getName(name)); } else { contentId(NO_NAME); } } return this; }
/** * Resolves file path depending on {@link Match matching type} * Returned path is formatted in unix style. */ protected String getMatchingFilePath(File file) { String path = null; switch (matchType) { case FULL_PATH: path = file.getAbsolutePath(); break; case RELATIVE_PATH: path = file.getAbsolutePath(); path = path.substring(rootPath.length()); break; case NAME: path = file.getName(); } path = FileNameUtil.separatorsToUnix(path); return path; }
/** * Converts all separators to the system separator. * * @param path the path to be changed, null ignored * @return the updated path */ public static String separatorsToSystem(final String path) { if (path == null) { return null; } if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) { return separatorsToWindows(path); } else { return separatorsToUnix(path); } }
/** * Does the work of getting the path. * * @param filename the filename * @param separatorAdd 0 to omit the end separator, 1 to return it * @return the path */ private static String doGetPath(final String filename, final int separatorAdd) { if (filename == null) { return null; } int prefix = getPrefixLength(filename); if (prefix < 0) { return null; } int index = indexOfLastSeparator(filename); int endIndex = index + separatorAdd; if (prefix >= filename.length() || index < 0 || prefix >= endIndex) { return StringPool.EMPTY; } return filename.substring(prefix, endIndex); }