Java 如何从uri确定文件的文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3234090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine the file extension of a file from a uri
提问by Ankur
Assuming I am given a URI, and I want to find the file extension of the file that is returned, what do I have to do in Java.
假设我得到了一个 URI,并且我想找到返回文件的文件扩展名,我必须在 Java 中做什么。
For example the file at http://www.daml.org/2001/08/baseball/baseball-ontis http://www.daml.org/2001/08/baseball/baseball-ont.owl
例如http://www.daml.org/2001/08/baseball/baseball-ont的文件是http://www.daml.org/2001/08/baseball/baseball-ont.owl
When I do
当我做
URI uri = new URI(address);
URL url = uri.toURL();
String file = url.getFile();
System.out.println(file);
I am not able to see the full file name with .owl
extension, just /2001/08/baseball/baseball-ont
how do I get the file extension as well.
``
我看不到带.owl
扩展名的完整文件名,/2001/08/baseball/baseball-ont
我如何获得文件扩展名。``
采纳答案by Tim Visée
At first, I want to make sure you know it's impossible to find out what kind of file a URI links too, since a link ending with .jpg
might let you access a .exe
file (this is especially true for URL's, due to symbolic links and .htaccess files), thus it isn't a rock solid solution to fetch the realextension from the URI if you want to limit allowed file types, if this is what you're going for of course. So, I assume you just want to know what extension a file has based on it's URI even though this isn't completely trustworthy;
首先,我要确保你知道这是不可能找出什么样的文件的URI链接太多,因为结尾的链接.jpg
可以让你访问一个.exe
文件(这是URL的尤其如此,因为符号链接和的.htaccess文件),因此,如果您想限制允许的文件类型,那么从 URI获取真正的扩展名并不是一个坚如磐石的解决方案,当然,如果这是您想要的。因此,我假设您只想知道基于 URI 的文件具有什么扩展名,即使这并不完全值得信赖;
You can get the extension from any URI, URL or file path using the method bellow. You don't have to use any libraries or extensions, since this is basic Java functionality. This solution get's the position of the last .
(period) sign in the URI string, and creates a sub-string starting at the position of the period sign, ending at the end of the URI string.
您可以使用以下方法从任何 URI、URL 或文件路径获取扩展名。您不必使用任何库或扩展,因为这是基本的 Java 功能。此解决方案获取.
URI 字符串中最后一个(句点)符号的位置,并创建一个从句点符号位置开始,到 URI 字符串末尾结束的子字符串。
String uri = "http://www.google.com/support/enterprise/static/gsa/docs/admin/70/gsa_doc_set/integrating_apps/images/google_logo.png";
String extension = uri.substring(uri.lastIndexOf("."));
This code sample will above will output the .png
extension from the URI in the extension
variable, note that a .
(period) is included in the extension, if you want to gather the file extension without a prefixed period, increase the substring index by one, like this:
上面这个代码示例会.png
从extension
变量中的URI输出扩展名,注意.
扩展名中包含一个(句点),如果你想收集没有前缀句点的文件扩展名,将子字符串索引增加1,像这样:
String extension = uri.substring(url.lastIndexOf(".") + 1);
One pro for using this method over regular expressions (a method other people use a lot) is that this is a lot less resource expensive and a lot less heavy to execute while giving the same result.
在正则表达式(其他人经常使用的一种方法)上使用此方法的一个优点是,在给出相同结果的同时,资源消耗要少得多,执行起来也少得多。
Additionally, you might want to make sure the URL contains a period character, use the following code to achieve this:
此外,您可能希望确保 URL 包含句点字符,请使用以下代码来实现此目的:
String uri = "http://www.google.com/support/enterprise/static/gsa/docs/admin/70/gsa_doc_set/integrating_apps/images/google_logo.png";
if(uri.contains(".")) {
String extension = uri.substring(url.lastIndexOf("."));
}
You might want to improve the functionally even further to create a more robust system. Two examples might be:
您可能希望进一步改进功能以创建更强大的系统。两个例子可能是:
- Validate the URI by checking it exists, or by making sure the syntax of the URI is valid, possibly using a regular expression.
- Trim the extension to remove unwanted white spaces.
- 通过检查 URI 是否存在来验证 URI,或者通过确保 URI 的语法有效,可能使用正则表达式。
- 修剪扩展以删除不需要的空格。
I won't cover the solutions for these two features in here, because that isn't what was being asked in the first place.
我不会在这里介绍这两个功能的解决方案,因为这不是首先要问的问题。
Hope this helps!
希望这可以帮助!
回答by Stephen C
There are two answers to this.
对此有两个答案。
If a URI does not have a "file extension", then there is no way that you can infer one by looking at it textually, or by converting it to a File
. In general, neither the URI or the File needs to have an extension at all. Extensions are just a file naming convention.
如果 URI 没有“文件扩展名”,则您无法通过查看文本或将其转换为File
. 通常,URI 或文件都不需要扩展名。扩展名只是一种文件命名约定。
What you are really after is the media type / MIMEtype / content type of the file. You may be able to determine the media type by doing something like this:
您真正想要的是文件的媒体类型/MIMEtype/内容类型。您可以通过执行以下操作来确定媒体类型:
URLConnection conn = url.connect();
String type = conn.getContentType();
However the getContentType()
method will return null
if the server did not set a content type in the response. (Or it could give you the wrong content type, or a non-specific content type.) At that point, you would need to resort to content type "guessing", and I don't know if that would give you a specific enough type in this case.
但是,如果服务器未在响应中设置内容类型,则该getContentType()
方法将返回null
。(或者它可能会给你错误的内容类型,或非特定的内容类型。)在这一点上,你需要诉诸内容类型“猜测”,我不知道这是否会给你一个足够具体的在这种情况下键入。
But if you "know" that the file shouldbe OWL, why don't you just give it a ".owl" extension anyway?
但是,如果您“知道”该文件应该是 OWL,为什么不给它一个“.owl”扩展名呢?
回答by Joop Eggen
URLConnection.guessContentTypeFromName(url)
would deliver the mime type as in the first answer.
Maybe you simply wanted:
URLConnection.guessContentTypeFromName(url)
将提供第一个答案中的 mime 类型。也许你只是想要:
String extension = url.getPath().replaceFirst("^.*/[^/]*(\.[^\./]*|)$", "");
The regular expression consuming all upto the last slash, then upto a period and either returns an extension like ".owl" or "". (If not mistaken)
正则表达式消耗所有直到最后一个斜杠,然后直到一个句点并返回一个扩展名,如“.owl”或“”。(如果没记错的话)
回答by Aaron
This link might help for those who are still having problems: How I can get the mime type of a file having its Uri?
此链接可能对仍有问题的人有所帮助: 如何获取具有 Uri 的文件的 MIME 类型?
public static String getMimeType(Context context, Uri uri) {
String extension;
//Check uri format to avoid null
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
//If scheme is a content
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
} else {
//If scheme is a File
//This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
}
return extension;
}
回答by Vikasdeep Singh
I am doing it in this way.
我就是这样做的。
You can check any file extension with more validation:
您可以通过更多验证检查任何文件扩展名:
String stringUri = uri.toString();
String fileFormat = "png";
if (stringUri.contains(".") && fileFormat.equalsIgnoreCase(stringUri.substring(stringUri.lastIndexOf(".") + 1))) {
// do anything
} else {
// invalid file
}
回答by sdgfsdh
As other answers have explained, you don't really know the content type without inspecting the file. However, you can predict the file type from a URL.
正如其他答案所解释的那样,如果不检查文件,您就不会真正了解内容类型。但是,您可以从 URL 预测文件类型。
Java almostprovides this functionality as part of the URL
class. The method URL::getFile
will intelligently grab the file portion of a URL
:
Java几乎将这个功能作为URL
类的一部分提供。该方法URL::getFile
将智能地抓取 a 的文件部分URL
:
final URL url = new URL("http://www.example.com/a/b/c/stuff.zip?u=1");
final String file = url.getFile(); // file = "/a/b/c/stuff.zip?u=1"
We can use this to write our implementation:
我们可以使用它来编写我们的实现:
public static Optional<String> getFileExtension(final URL url) {
Objects.requireNonNull(url, "url is null");
final String file = url.getFile();
if (file.contains(".")) {
final String sub = file.substring(file.lastIndexOf('.') + 1);
if (sub.length() == 0) {
return Optional.empty();
}
if (sub.contains("?")) {
return Optional.of(sub.substring(0, sub.indexOf('?')));
}
return Optional.of(sub);
}
return Optional.empty();
}
This implementation should handle edge-cases properly:
此实现应正确处理边缘情况:
assertEquals(
Optional.of("zip"),
getFileExtension(new URL("http://www.example.com/stuff.zip")));
assertEquals(
Optional.of("zip"),
getFileExtension(new URL("http://www.example.com/stuff.zip")));
assertEquals(
Optional.of("zip"),
getFileExtension(new URL("http://www.example.com/a/b/c/stuff.zip")));
assertEquals(
Optional.empty(),
getFileExtension(new URL("http://www.example.com")));
assertEquals(
Optional.empty(),
getFileExtension(new URL("http://www.example.com/")));
assertEquals(
Optional.empty(),
getFileExtension(new URL("http://www.example.com/.")));
回答by Nevil Ghelani
Accepted answer is not useful for url contains '?' or '/' after extension. So, to remove that extra string, You can use getLastPathSegment() method. It gives you only name from uri and then you can get extension as follows:
接受的答案对包含 '?' 的 url 没有用 或扩展名后的“/”。因此,要删除该额外字符串,您可以使用 getLastPathSegment() 方法。它只给你来自 uri 的名字,然后你可以获得扩展名,如下所示:
String name = uri.getLastPathSegment();
//Here uri is your uri from which you want to get extension
String extension = name.substring(name.lastIndexOf("."));
above code gets extension with .(dot) if you want to remove the dot then you can code as follows:
上面的代码使用 .(dot) 扩展,如果你想删除点,那么你可以编码如下:
String extension = name.substring(name.lastIndexOf(".") + 1);
回答by Asad Ali Choudhry
Another useful way which is not mentioned in accepted answer is, If you have a remote url, then you can get mimeType from URLConnection, Like
接受的答案中没有提到的另一种有用的方法是,如果您有一个远程网址,那么您可以从 URLConnection 中获取 mimeType,例如
URLConnection urlConnection = new URL("http://www.google.com").openConnection();
String mimeType = urlConnection.getContentType();
Now to get file extension from MimeType, I'll refer to this post
现在要从 MimeType 获取文件扩展名,我将参考这篇文章