有没有办法从URL获取文件扩展名

z3n*_*10n 16 .net vb.net url file

我想知道,为了确保将从我的脚本下载的文件将具有我想要的扩展名.

该文件不会在以下网址:

http://example.com/this_url_will_download_a_file
Run Code Online (Sandbox Code Playgroud)

或许是的,但是,我认为我只会使用那种URL:

http://example.com/file.jpg
Run Code Online (Sandbox Code Playgroud)

我不会用它来检查:Url.Substring(Url.LastIndexOf(".") - 3, 3)因为这是一种非常糟糕的方式.

那么,你建议我做什么?

Ale*_*lex 15

这是我使用的一个简单的。使用参数、绝对和相对 URL 等。

public static string GetFileExtensionFromUrl(string url)
{
    url = url.Split('?')[0];
    url = url.Split('/').Last();
    return url.Contains('.') ? url.Substring(url.LastIndexOf('.')) : "";
}
Run Code Online (Sandbox Code Playgroud)

单元测试,如果你愿意

[TestMethod]
public void TestGetExt()
{
    Assert.IsTrue(Helpers.GetFileExtensionFromUrl("../wtf.js?x=wtf")==".js");
    Assert.IsTrue(Helpers.GetFileExtensionFromUrl("wtf.js")==".js");
    Assert.IsTrue(Helpers.GetFileExtensionFromUrl("http://www.com/wtf.js?wtf")==".js");
    Assert.IsTrue(Helpers.GetFileExtensionFromUrl("wtf") == "");
    Assert.IsTrue(Helpers.GetFileExtensionFromUrl("") == "");
}
Run Code Online (Sandbox Code Playgroud)

根据您自己的需要进行调整。

PS 不要使用Path.GetExtension因为它不适用于查询字符串参数


her*_*ger 12

这很奇怪,但它有效:

string url = @"http://example.com/file.jpg";
string ext = System.IO.Path.GetExtension(url);
MessageBox.Show(this, ext);
Run Code Online (Sandbox Code Playgroud)

但是,正如crono所说,它不适用于参数:

string url = @"http://example.com/file.jpg?par=x";
string ext = System.IO.Path.GetExtension(url);
MessageBox.Show(this, ext);
Run Code Online (Sandbox Code Playgroud)

结果:".jpg?par = x"


Ste*_*ono 5

我知道这是一个老问题,但对看到这个问题的人可能会有所帮助。

从 URL 中的文件名获取扩展名的最佳方法也是使用正则表达式。

您可以使用此模式(不仅仅是网址):

.+(\.\w{3})\?*.*
Run Code Online (Sandbox Code Playgroud)

解释:

.+     Match any character between one and infinite
(...)  With this, you create a group, after you can use for getting string inside the brackets
\.     Match the character '.'
\w     Matches any word character equal to [a-zA-Z0-9_]
\?*    Match the character '?' between zero and infinite
.*     Match any character between zero and infinite
Run Code Online (Sandbox Code Playgroud)

例子:

http://example.com/file.png
http://example.com/file.png?foo=10

But if you have an URL like this:

http://example.com/asd
This take '.com' as extension.
Run Code Online (Sandbox Code Playgroud)

因此,您可以对这样的 url 使用强模式:

.+\/{2}.+\/{1}.+(\.\w+)\?*.*
Run Code Online (Sandbox Code Playgroud)

解释:

.+        Match any character between one and infinite
\/{2}     Match two '/' characters
.+        Match any character between one and infinite
\/{1}     Match one '/' character
.+        Match any character between one and infinite
(\.\w+)  Group and match '.' character and any word character equal to [a-zA-Z0-9_] from one to infinite
\?*       Match the character '?' between zero and infinite
.*        Match any character between zero and infinite
Run Code Online (Sandbox Code Playgroud)

例子:

http://example.com/file.png          (Match .png)
https://example.com/file.png?foo=10  (Match .png)
http://example.com/asd               (No match)
C:\Foo\file.png                      (No match, only urls!)

http://example.com/file.png

    http:        .+
    //           \/{2}
    example.com  .+
    /            \/{1}
    file         .+
    .png         (\.\w+)
Run Code Online (Sandbox Code Playgroud)