gpt4 book ai didi

java - 下载 torrent java 时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:42:42 24 4
gpt4 key购买 nike

我正在尝试构建一个java torrent下载器,基于ttorrent java 库(链接到文档和基本说明)。

我完全按照 javadoc 中列出的说明进行操作。运行代码时,似乎没有发生任何事情。也不异常(exception)。显然该文件没有下载,我什至在下载文件夹中看不到“.part”文件。保持在 0%。

这是我到目前为止的代码(将其与文档中的代码进行比较)。

 Client client = new Client(

InetAddress.getLocalHost(),

SharedTorrent.fromFile(
new File(TORRENTPATH),
new File(MKPATH)));

client.download();
client.waitForCompletion();

我没有找到比那个更好的 ttorrent 文档。我究竟做错了什么?当然,该种子有种子,因为我之前从另一个种子管理器下载过它。

更新:虽然未下载任何内容,但下载过程似乎已开始,但会抛出以下日志消息:

enter image description here

最佳答案

我知道其中的示例不太好,但这里是一个很好的起点:

TorrentTest.java

import jargs.gnu.CmdLineParser;

import java.io.File;
import java.io.PrintStream;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.BasicConfigurator;

import com.turn.ttorrent.client.Client;
import com.turn.ttorrent.client.Client.ClientState;
import com.turn.ttorrent.client.SharedTorrent;
public class TorrentTest {

public static final String DEFAULT_TRACKER_URI = "http://localhost:6969/announce";

/**
* Display program usage on the given {@link PrintStream}.
*
*/
private static void usage(PrintStream s)
{
s.println("usage: SimpleClient [options] torrent");
s.println("Leech and seed this torrent file.");
s.println();
s.println("Available options:");
s.println(" -h,--help Show this help and exit.");
s.println(" -o,--output DIR Output directory for file.");
s.println();
}

/**
* Main program function.
*
* @param args
*/
public static void main(String[] args)
{
BasicConfigurator.configure();

CmdLineParser parser = new CmdLineParser();
CmdLineParser.Option help = parser.addBooleanOption('h', "help");
CmdLineParser.Option outputString = parser.addStringOption('o', "output");

try {
parser.parse(args);
} catch (CmdLineParser.OptionException oe) {
System.err.println(oe.getMessage());
usage(System.err);
System.exit(1);
}

// Display help and exit if requested
if (Boolean.TRUE.equals((Boolean)parser.getOptionValue(help))) {
usage(System.out);
System.exit(0);
}

// Get options
File output = new File((String) parser.getOptionValue(outputString, "."));

// Check that it's the correct usage
String[] otherArgs = parser.getRemainingArgs();
if (otherArgs.length != 1) {
usage(System.err);
System.exit(1);
}

// Get the .torrent file path
File torrentPath = new File(otherArgs[0]);

// Start downloading file
try {
SharedTorrent torrent = SharedTorrent.fromFile(torrentPath, output);
System.out.println("Starting client for torrent: "+torrent.getName());
Client client = new Client(InetAddress.getLocalHost(), torrent);

try {
System.out.println("Start to download: "+torrent.getName());
client.share(); // SEEDING for completion signal
// client.download() // DONE for completion signal

while (!ClientState.SEEDING.equals(client.getState())) {
// Check if there's an error
if (ClientState.ERROR.equals(client.getState())) {
throw new Exception("ttorrent client Error State");
}

// Display statistics
System.out.printf("%f %% - %d bytes downloaded - %d bytes uploaded\n", torrent.getCompletion(), torrent.getDownloaded(), torrent.getUploaded());

// Wait one second
TimeUnit.SECONDS.sleep(1);
}

System.out.println("download completed.");
} catch (Exception e) {
System.err.println("An error occurs...");
e.printStackTrace(System.err);
} finally {
System.out.println("stop client.");
client.stop();
}
} catch (Exception e) {
System.err.println("An error occurs...");
e.printStackTrace(System.err);
}
}
}

关于java - 下载 torrent java 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24458854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com