java - 下载 .torrent 文件 Java

标签 java download bittorrent

我正在尝试用 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/

相关文章:

java - 如何在捕获 JPanel 时仅捕获选定的组件图像?

java正则表达式用于读取特定符号后的数字

python - 使用 asyncio 与其他对等方连接时如何处理 ConnectionRefusedError

java - Jacoco 代理 - 无输出

java swing 与圆点的交线

node.js - 使用下载管理器取消下载时 Nodejs 内存泄漏

apache-flex - 使用 FileReference 下载时丢失扩展名

PHP/Apache 拒绝用户访问文件夹但不访问脚本

java - 向 Bittorrent 跟踪器发送 HTTP Get - info_hash 和对等 ID 在哪里?

c - 为什么我从对等方接收到的数据与预期输出不匹配?