URLLocator implements Locator<URLLocation> { @CheckForNull static String getFilenameFromContentDispositionHeader(@Nullable String header) { if (isEmpty(header)) { return null; } String filename = header.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1"); if (header.equals(filename)) { filename = StringUtils.substringAfterLast(header, "="); } if (filename != null) { filename = StringUtils.remove(filename, "\""); filename = StringUtils.remove(filename, "'"); filename = StringUtils.remove(filename, ";"); filename = StringUtils.remove(filename, " "); } return filename; }  @Override File locate(URLLocation location); @Override File copyToDirectory(URLLocation location, File toDir); @Override InputStream openInputStream(URLLocation location); @Override File copyToFile(URLLocation location, File toFile);  }
@Test public void test_getFilenameFromContentDispositionHeader() { assertThat(URLLocator.getFilenameFromContentDispositionHeader(null)).isNull(); assertThat(URLLocator.getFilenameFromContentDispositionHeader("")).isNull(); assertThat(URLLocator.getFilenameFromContentDispositionHeader("Content-Disposition: attachment; filename=foo.jar")).isEqualTo("foo.jar"); assertThat(URLLocator.getFilenameFromContentDispositionHeader("Content-Disposition: attachment; filename=foo.jar;")).isEqualTo("foo.jar"); }