下载种子java的问题

时间:2014-06-27 18:44:26

标签: java bittorrent

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

我按照javadoc中列出的说明完全按照原样进行操作。运行代码时,似乎没有发生任何事情。也没有例外。显然文件没有下载,我甚至看不到" .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

1 个答案:

答案 0 :(得分:1)

我知道那些例子并不是很好,但这里有一个对你来说很好的起点:

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);
        }
    }
}