2014-02-26 92 views
2

我正在試圖下載Java中的.torrent文件。我提到了這個SO(Java .torrent file download)的問題,但是當我運行該程序時,它並沒有開始下載。它絕對沒有。有人可以向我解釋我做錯了什麼嗎?我在下面發佈了一個SSCCE。先謝謝了。下載.torrent文件Java

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

回答

1

您沒有格式化正確的文件路徑。在這部分代碼是你的問題:

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

它仍然無法正常工作,我的路徑有問題嗎? – Joe

+0

查看我更新的帖子。 – uyuyuy99

+0

還有一個問題,我怎樣才能使我的路徑更通用,因此它適用於每臺計算機,因爲現在,我的名字在路徑中。 – Joe