我正在用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();
}
}发布于 2014-02-26 23:33:39
您没有正确设置文件路径的格式。代码的这一部分是您的问题:
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();
}https://stackoverflow.com/questions/22055649
复制相似问题