简体   繁体   English

如何查看网址http://www.test.com/abc.pdf是文件还是目录?

[英]how to find out if the url http://www.test.com/abc.pdf is a file or a directory?

i want to write some code in java to find out if a given url is a file or a directory. 我想在java中编写一些代码,以确定给定的URL是文件还是目录。 how can i do this?? 我怎样才能做到这一点??

URLs themselves don't have the concept of being a "file" or a "directory". URL本身不具有“文件”或“目录”的概念。 The content of a URL is defined by whatever the server responds with when requested. URL的内容由服务器在请求时响应的任何内容定义。 If you get something with a MIME type of application/pdf , then the URL represents a PDF file. 如果您获得MIME类型为application/pdf ,则URL表示PDF文件。 If you get anything else, then it's not a PDF file. 如果你得到任何其他东西,那么它不是PDF文件。

There is simply no notion of a "directory" in any of the URL / URI specs, the HTTP specs or the MIME type registry. 在任何URL / URI规范,HTTP规范或MIME类型注册表中都没有“目录”的概念。

So the webserver has no way of telling the client that a URL resolves to a directory ... even if it knows what that means. 因此,网络服务器无法告诉客户端 URL解析为目录......即使它知道这意味着什么。 (And in many cases, the webserver doesn't know / care about directories itself; eg a typical RESTful web API doesn't recognize the concept.) (在许多情况下,Web服务器不知道/关心目录本身;例如,典型的RESTful Web API无法识别该概念。)

Your options are: 你的选择是:

  • Try to fetch things and see what content type you get. 尝试获取内容并查看您获得的内容类型。 But bear in mind that a "directory" might be rendered by the webserver as anything ... so it is (in general) impossible to reliably distinguish directories from non-directories this way. 但请记住,网络服务器可能会将“目录”呈现为任何内容......因此(通常)不可能以这种方式可靠地区分目录和非目录。

    If you wanted to avoid downloading the file, you could send a HEAD request instead of a GET request. 如果您想避免下载文件,可以发送HEAD请求而不是GET请求。 This requires the use of a fully fledged HTTP client library rather than URLConnection . 这需要使用完全成熟的HTTP客户端库而不是URLConnection

  • Change your application design and implementation so that the "directory" concept is not required. 更改您的应用程序设计和实现,以便不需要“目录”概念。

  • Change your application so that it decides what is a "directory" and what is a "file" based purely on the URLs. 更改您的应用程序,以便决定什么是“目录”,什么是纯粹基于URL的“文件”。 (In the general case, this won't work ... because there are no universally observed conventions for URL name parts that would allow you to make the distinction.) (在一般情况下,这将不起作用......因为没有普遍观察到的URL名称部分的约定,这将允许您进行区分。)

  • Change to using a URL scheme / protocol in which "directory" is a well-defined concept. 更改为使用URL方案/协议,其中“目录”是明确定义的概念。 For instance "file:" or "ftp:". 例如“file:”或“ftp:”。

What you get back from a URL essentially isn't a "file" or a "directory." 从URL返回的内容本质上不是“文件”或“目录”。 At best, it's a stream of data with a content type. 充其量,它是具有内容类型的数据流。 It generally becomes a "file" on the client side, either by means of saving it to a file system or to a temporary store for display only. 它通常成为客户端的“文件”,通过将其保存到文件系统或临时存储仅供显示。 Basically, there's no way for a web server to tell a client that something is a directory using HTTP. 基本上,Web服务器无法告诉客户端某些东西是使用HTTP的目录。

You're either going to have to create some client-side business logic to infer a "directory" (possibly based on the URL, maybe a lack of file extension?) or use a different protocol for this. 您要么必须创建一些客户端业务逻辑来推断“目录”(可能基于URL,可能缺少文件扩展名?)或使用不同的协议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java如何确定URL是http还是https? - Java how to find out if a URL is http or https? 哪里可以找到http://www.alibaba.com api的英文信息? - Where to find information on http://www.alibaba.com api in english? 找不到http://www.objectdb.com/eclipse - could not find http://www.objectdb.com/eclipse 如何通过提供URL来查找网站中断开的链接,例如“ www.hammacher.com” - How to find the broken links in a website by providing the URL say for example 'www.hammacher.com' 如何访问http:// localhost:8080 / pdf / abc123.pdf tomcat eclipse - How to access http://localhost:8080/pdf/abc123.pdf tomcat eclipse 我可以在firefox中打开URL'http://www.demo.guru99.com/V4/',但是当我在Eclipse中运行selenium脚本时,它显示“连接已超时” - I am able to open URL'http://www.demo.guru99.com/V4/' in firefox,but when I run selenium script in eclipse it shows 'The connection has timed out' 如何通过 selenium-webdriver 在 url http://www.spicejet.com/ 上执行多个操作并单击带有文本的链接作为会员登录 - How to perform multiple actions and click on the link with text as Member Login on the url http://www.spicejet.com/ through selenium-webdriver 如果File不是文件或目录,如何找出它? - How can I find out if a File is a file or directory if it does not exist? 如何从servlet到达www目录中的文件? - how to reach a file in www directory from a servlet? 如何在不创建 object 的情况下找出 java 中文件和目录的大小? - how to find out the size of file and directory in java without creating the object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM