[英]Finding current size of file vs. amount in title of file java
我一直在從事一個涉及下載種子的小項目。 最初,我計划使用ttorrent(Java BitTorrent庫)下載所需的Torrent,但是與uTorrent相比,它是比較慢的方法。 我決定通過執行命令行來使用uTorrent。
Process p=Runtime.getRuntime().exec("cmd /c cd C:\\Users\\Administrator\\AppData\\Roaming\\uTorrent & start uTorrent.exe /DIRECTORY C:\\ C:\\"+ CURRENT_DOWNLOADS + ".torrent");
p.waitFor();
但是,由於這解決了我的速度問題,它使我無法控制任何事情,例如下載完成時。 我需要知道這一點,因為完成下載后,我應該會收到一封郵件到我的電子郵件中。 由於我不知道文件何時完成,因此在文件開始下載后不久就會發送電子郵件。 下載種子文件時,它將顯示為完整文件的文件長度,並且標題沒有變化,例如添加的后綴或其他內容。
我需要一種方法來計算文件的實際大小與標題中文件的大小,或者基於文件/文件夾中的更改來知道何時完成文件下載的另一種方式
到目前為止的一段代碼。
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
folder = new File(path);
listOfFiles = folder.listFiles();
// Beginning of file search loop
boolean fileDownloaded = false;
while (!fileDownloaded) {
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.contains(".mp4") && !files.contains(".part") && listOfFiles[i].length() > 838860800) {
//
File ff = listOfFiles[i];
if(ff.exists()) {
for(int timeout = 100; timeout>0; timeout--) {
RandomAccessFile ran = null;
try {
ran = new RandomAccessFile(ff, "rw");
System.out.println("1");
break; // no errors, done waiting
} catch (Exception ex) {
System.out.println("2");
} finally {
if(ran != null) try {
System.out.println("3");
ran.close();
} catch (IOException ex) {
//do nothing
}
ran = null;
}
try {
System.out.println("4");
Thread.sleep(100); // wait a bit then try again
} catch (InterruptedException ex) {
//do nothing
}
}
System.out.println("5");
} else {
}
try {
GoogleMail.Send("movdload", "********", "**********@gmail.com", "Downloaded!", "http://**********/"+subPath+"/"+listOfFiles[i].getName());
fileDownloaded = true;
} catch (MessagingException e) {
e.printStackTrace();
}
break;
}
}
}
Thread.currentThread().sleep(1000);
}
我認為一旦下載文件,您將無法直接從utorrent獲取信息。 但是您可以使用NIO2 java 7文件api來繼續檢查文件狀態,即具有連續間隔和文件大小的上次修改狀態。 由於torrent文件可能會提供有關要下載的文件大小的信息。 但是,如果大小不完全匹配,並且由於種子而導致種子卡死,那么就存在問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.