2

我想在我的项目中使用 ttorent java lib。我试图弄清楚它是如何工作的。当我想将它用作独立程序并调用时

./client -o ~ ~/file.torrent -i eth3

总是有 0%。当我尝试将它用作具有如下简单代码的库时:

import java.io.File;
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 Main {

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

    // Get options
    File output = new File("/home/user");

    // Get the .torrent file path
    File torrentPath = new File("/home/user/file.torrent");

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

}

它也总是 0%。我尝试使用其他客户端下载这个 torrent 文件并且没问题,所以我认为这不乏种子。

4

0 回答 0