Joe
Joe

Reputation: 111

Download .torrent file Java

I am trying to download a .torrent file in Java. I referred to this SO (Java .torrent file download) question but when I run the program it does not start the download. It does absolutely nothing. Can someone explain to me what I am doing wrong? I posted an SSCCE below.

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

Upvotes: 2

Views: 6377

Answers (1)

uyuyuy99
uyuyuy99

Reputation: 353

You didn't format the file path correct. This part of the code is your problem:

File f = new File(path + "t2.torrent");

Change it to this:

File f = new File(path + File.separator + "t2.torrent");

EDIT:

If that does not work, you should try fixing your file path. Are you sure it's not something like C:\Users\Bob\Documents?

Once you have your file path fixed and the torrent file is downloading correctly, if your torrent program throws an error when loading the torrent, it's likely because the .torrent file is in GZIP format. To fix that, just follow the solution posted on the question you linked to:

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

Upvotes: 1

Related Questions