Frage

I have been working on a little project involving downloading torrents. Originally I planned on using ttorrent (Java BitTorrent Library) to download the torrents I needed however it was way to slow compared to uTorrent. I decided to use uTorrent by executing a command line.

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

However, as this solved my speed issue, it prevents me from having control of anything such as when downloads finish. I need to know this because when it's done downloading, I am supposed to receive a message to my email. Since I don't know when the file is done, the email is sent shortly after the file starts downloading. When the torrent is being downloaded, it shows as the length of the file to be the full file and no variation in the title such as an added suffix or anything.

I NEED A WAY TO EITHER CALCULATE THE ACTUAL SIZE OF A FILE VS THE SIZE OF THE FILE IN THE HEADER OR AN ALTERNATIVE WAY TO KNOW WHEN THE FILE IS DONE DOWNLOADING BASED ON CHANGES IN THE FILE / FOLDER

A piece of code so far.

  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);
                }
War es hilfreich?

Lösung

I think you can't get the information directly from utorrent once the file is downloaded. But you can use NIO2 java 7 file apis to keep checking the file status i.e. last modified status with continuous interval and size of file. As torrent file might give the information about what's the file size which is to be downloaded. But If size doesn't match exactly and torrent is stuck because of seeds then there is a problem.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top