简体   繁体   English

从给定的URL下载文件时出错

[英]Error downloading file from a given URL

I am trying to download the given URL: http://www.addic7ed.com/original/9521/7 我正在尝试下载给定的URL: http://www.addic7ed.com/original/9521/7 : http://www.addic7ed.com/original/9521/7

but when i try to download the file using my Java Code: 但是当我尝试使用Java代码下载文件时:

URL url = new URL("http://www.addic7ed.com/original/9521/7");
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream("abc.srt"); 
fos.getChannel().transferFrom(rbc, 0, 1 << 24);

The html page gets downloaded whereas the file to be downloaded should be a .srt extension file. 将下载html page ,而要下载的文件应为.srt extension文件。

But when I try to download the above link using Internet Download Manager the file gets downloaded. 但是,当我尝试使用Internet Download Manager下载以上链接时,文件将被下载。

IDM converts the above given URL into http://www.addic7ed.com/srtcache/Supernatural/Supernatural%20-%2004x06%20-%20Yellow%20Fever.720p%20CTU.English.orig.Addic7ed.com.srt IDM将上述给定的URL转换为http://www.addic7ed.com/srtcache/Supernatural/Supernatural%20-%2004x06%20-%20Yellow%20Fever.720p%20CTU.English.orig.Addic7ed.com.srt

So my question is how to achieve this in JAVA...?? 所以我的问题是如何在JAVA中实现这一目标……? Are there any API available to achieve this. 是否有任何API可以实现这一目标。

Have you looked at the HTML file? 您看过HTML文件了吗? I suspect that it is actually an error page from the server, and that it contains clues about what is actually going on. 我怀疑这实际上是服务器上的错误页面,并且其中包含有关实际情况的线索。

Here are some possibilities: 这里有一些可能性:

  • Maybe you need to supply authentication credentials. 也许您需要提供身份验证凭据。

  • Maybe the server is sending a redirect (3xx) response, and that the client side is not performing the redirect. 也许服务器正在发送重定向(3xx)响应,而客户端未在执行重定向。

  • Maybe you need to set some extra headers to make the server realize that it should not turn the response as HTML. 也许您需要设置一些额外的标头,以使服务器意识到它不应将响应转换为HTML。 For example, an Accept header. 例如,Accept标头。

But note that the details will depend on the server that you are trying to talk to. 但是请注意,详细信息将取决于您尝试与之对话的服务器。


If I was trying to download files programmatically in Java, I would either use HttpUrlConnection or the Apache HttpClient libraries. 如果尝试使用Java以编程方式下载文件,则可以使用HttpUrlConnection或Apache HttpClient库。 Both will give you more control over the download process than simply using URL.openStream() 与简单地使用URL.openStream()相比,两者都将为您提供对下载过程的更多控制

This is probably because the http response from http://www.addic7ed.com/original/9521/7 is a 302 redirect which your java code is not correctly handling. 这可能是因为来自http://www.addic7ed.com/original/9521/7的http响应是302重定向,您的Java代码未正确处理。 IDM correctly handles the redirect. IDM正确处理重定向。 A great tool to use if you are on a *nix based systme, or have cygwin installed on windows is curl. 如果您使用的是基于* nix的系统,或者Windows上安装了cygwin,则一个不错的工具是curl。

curl -v http://myurl curl -v http:// myurl

will display all the http traffic information (request/response) 将显示所有http交通信息(请求/响应)

你试过了吗?

URL url = new URL("http://www.addic7ed.com/original/9521/7");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM