java 从 URL 检查内容类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28145559/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:04:16  来源:igfitidea点击:

Checking content type from URL

javaurlhttpurlconnectioncontent-type

提问by Anggrayudi H

I asked this questionbefore and Evgeniy Dorofeevanswered it. Although worked for direct link only, but I accepted his answer. He just told me about check the content type from direct link:

我之前问过这个问题Evgeniy Dorofeev回答了。虽然仅适用于直接链接,但我接受了他的回答。他只是告诉我从直接链接检查内容类型:

String requestUrl = "https://dl-ssl.google.com/android/repository/android-14_r04.zip";
URL url = new URL(requestUrl);
URLConnection c = url.openConnection();
String contentType = c.getContentType();

As far I know, there are two URLtypes to download a file:

据我所知,URL下载文件有两种类型:

  • Direct link. For example: https://dl-ssl.google.com/android/repository/android-14_r04.zip. From this link, we can download data directly and get the file name, included with file extension (in this link, .zipextension). So we can know what file to be downloaded. You can try to download from that link.
  • Undirect link. For example: http://www.example.com/directory/download?file=52378. Have you ever tried to download data from Google Drive? When downloading data from Google Drive, it will gives you an undirect link, such as the link above. We never know whether the link contains a file or webpage. Also, we don't know the file name and file extension is, because of this link type is unclear and random.
  • 直接链接。例如:https: //dl-ssl.google.com/android/repository/android-14_r04.zip。从这个链接,我们可以直接下载数据并获取文件名,包含文件扩展名(在这个链接中,.zip扩展名)。这样我们就可以知道要下载什么文件了。您可以尝试从该链接下载。
  • 非直接链接。例如:http://www.example.com/directory/download?file=52378。您是否尝试过从 Google Drive 下载数据?从 Google Drive 下载数据时,它会给您一个非直接链接,例如上面的链接。我们永远不知道该链接是否包含文件或网页。另外,我们不知道文件名和文件扩展名是什么,因为这种链接类型不清楚和随机。

I need to check whether it is a file or webpage. I must download it if the content type is a file.

我需要检查它是文件还是网页。如果内容类型是文件,我必须下载它。

So my question:

所以我的问题:

  • How do I check the content type from an undirect link?
  • As shown in the comments of this question, can HTTP-redirects solves the problem?
  • 如何从非直接链接检查内容类型?
  • 如该问题的评论所示,HTTP-redirects 可以解决问题吗?

Thanks for your help.

谢谢你的帮助。

回答by omerfarukdogan

After you open an URLConnection, a header file is returned. There are some information about the file in it. You can pull what you want from there. For example:

打开 URLConnection 后,将返回一个头文件。里面有一些关于文件的信息。你可以从那里拉出你想要的东西。例如:

URLConnection u = url.openConnection();
long length = Long.parseLong(u.getHeaderField("Content-Length"));
String type = u.getHeaderField("Content-Type");

lengthis size of the file in bytes, typeis something like application/x-dosexecor application/x-rar.

length是以字节type为单位的文件大小,类似于application/x-dosexecapplication/x-rar

回答by Malt

Such links redirect browsers to the actual content using HTTP redirects. To get the correct content type, all you have to do is tell HttpURLConnectionto follow the redirects by setting setFollowRedirects()to true (documented here).

此类链接使用HTTP 重定向将浏览器重定向到实际内容。要获得正确的内容类型,您所要做的就是HttpURLConnection通过设置setFollowRedirects()为 true(在此处记录)来告诉跟随重定向。

回答by Karan Datwani

MimeTypeMap.getFileExtensionFromUrl(url)

MimeTypeMap.getFileExtensionFromUrl(url)