【发布时间】:2014-05-28 01:18:33
【问题描述】:
我尝试了两种不同的方法来实现这一点,第一种:
public class DownloadTest {
public static void main(String[] args) {
URL website;
try {
website = new URL("http://re.zoink.it/067c4cc99A");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("vaca.torrent");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (Exception e) {
Logger.getLogger(DownloadTest.class.getName()).log(Level.SEVERE, null, e);
}
}
}
第二个:
public class DownloadTest2 {
public static void main(String args[]) throws IOException
{
String Episode = "http://re.zoink.it/067c4cc99A";
String Episode_save = "vaca.torrent";
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(Episode).openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(Episode_save);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos);
byte data[] = new byte[1024];
int read;
while((read = in.read(data,0,1024))>=0)
{
bout.write(data, 0, read);
}
bout.close();
in.close();
}
}
如果您在浏览器中使用该链接,您将收到一个对话框来下载它。或者如果你尝试:view-source:http://re.zoink.it/067c4cc99A,你可以看到它是一个真正的torrent文件。
这两种方法都不适用于此链接(我得到了一个损坏的文件),但是它们确实适用于其他链接,例如http://www.bt-chat.com/download1.php?id=192039&type=torrent
我的问题是如何从第一个下载文件?
【问题讨论】:
-
第二个示例中的代码似乎是做事的一般方式。您是否查看了以您结尾的文件,您有没有
diff'd它与您手动下载时得到的一样?也许链接首先重定向到其他链接? -
diff'd是什么意思?我所知道的是大小是相同的(手动和使用java),但我通过java得到的那个是损坏的。 (我试图用一个共同的客户打开两者) -
有什么不同?你有完全相同的字节数吗?顺便说一句,您为什么使用缓冲输入和输出?