我正在尝试用 Java 下载 .torrent 文件。我提到了这个 SO ( Java .torrent file download) 问题,但是当我运行该程序时,它不会开始下载。它什么都不做。有人可以向我解释我做错了什么吗?我在下面发布了一个 SSCCE。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class Test {
public static void main(String[] args) throws IOException {
String link = "http://torrage.com/torrent/13764753227BCBE3E8E82C058A7D5CE2BDDF9857.torrent";
String path = "/Users/Bob/Documents";
URL website = new URL(link);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
File f = new File(path + "t2.torrent");
FileOutputStream fos = new FileOutputStream(f);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
}
}
最佳答案
您没有正确格式化文件路径。这部分代码是你的问题:
File f = new File(path + "t2.torrent");
改成这样:
File f = new File(path + File.separator + "t2.torrent");
编辑:
如果这不起作用,您应该尝试修复您的文件路径。您确定它不是 C:\Users\Bob\Documents
之类的东西吗?
一旦您修复了文件路径并且 torrent 文件正在正确下载,如果您的 torrent 程序在加载 torrent 时抛出错误,这可能是因为 .torrent 文件是 GZIP 格式。要解决此问题,只需按照您链接到的问题上发布的解决方案操作即可:
String link = "http://torrage.com/torrent/13764753227BCBE3E8E82C058A7D5CE2BDDF9857.torrent";
String path = "/Users/Bob/Documents";
URL website = new URL(link);
try (InputStream is = new GZIPInputStream(website.openStream())) {
Files.copy(is, Paths.get(path + File.separator + "t2.torrent"));
is.close();
}
关于java - 下载 .torrent 文件 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22055649/