1

as you have seen before I'm working on a download manager in java, I have asked This Question and I have read This Question But These hadn't solve my problem. now I have wrote another code in java. but there is a problem. when download finishes file is larger than it's size and related software can't read it

This is image of my code execution :

enter image description here

as you see file size is about 9.43 MB

This is My project directory's image:

enter image description here

as you see my downloaded filesize is about 13 MB

So what is my Prooblem?

here is my complete source code

Main Class:

package download.manager;

import java.util.Scanner;

/**
 *
 * @author Behzad
 */
public class DownloadManager {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {        
        Scanner input = new Scanner(System.in);
        System.out.print("Enter url here : ");
        String url = input.nextLine();
        DownloadInfo information = new DownloadInfo(url);

    }
}

DownloadInfo Class:

package download.manager;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DownloadInfo {         

    private String downloadUrl;

    private String fileName;       

    private String fileExtension;

    private URL nonStringUrl;

    private HttpURLConnection connection;

    private int fileSize;

    private int remainingByte;

    private RandomAccessFile outputFile;

    public DownloadInfo(String downloadUrl) {
        this.downloadUrl = downloadUrl;

        initiateInformation();
    }

    private void initiateInformation(){        
        fileName = downloadUrl.substring(downloadUrl.lastIndexOf('/') + 1, downloadUrl.length());

        fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length());

        try {

            nonStringUrl = new URL(downloadUrl);

            connection = (HttpURLConnection) nonStringUrl.openConnection();

            fileSize = ((connection.getContentLength()));

            System.out.printf("File Size is : %d \n", fileSize);

            System.out.printf("Remain File Size is : %d \n", fileSize % 8);

            remainingByte = fileSize % 8;

            fileSize /= 8;

            outputFile = new RandomAccessFile(fileName, "rw");

        } catch (MalformedURLException ex) {
            Logger.getLogger(DownloadInfo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DownloadInfo.class.getName()).log(Level.SEVERE, null, ex);
        }



        System.out.printf("File Name is : %s\n", fileName);
        System.out.printf("File Extension is : %s\n", fileExtension);        
        System.out.printf("Partition Size is : %d MB\n", fileSize);

        int first = 0 , last = fileSize - 1;

        ExecutorService thread_pool = Executors.newFixedThreadPool(8);        

        for(int i=0;i<8;i++){            
            if(i != 7){
                thread_pool.submit(new Downloader(nonStringUrl, first, last, (i+1), outputFile));
            }
            else{
                thread_pool.submit(new Downloader(nonStringUrl, first, last + remainingByte, (i+1), outputFile));
            }
            first = last + 1;
            last += fileSize;
        }
        thread_pool.shutdown();

        try {
            thread_pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ex) {
            Logger.getLogger(DownloadInfo.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

and this is my downloader class:

package download.manager;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Behzad
 */
public class Downloader implements Runnable{

    private URL downloadURL;    

    private int startByte;       

    private int endByte;

    private int threadNum;    

    private RandomAccessFile outputFile;

    private InputStream stream;

    public Downloader(URL downloadURL,int startByte, int endByte, int threadNum, RandomAccessFile outputFile) {
        this.downloadURL = downloadURL;
        this.startByte = startByte;
        this.endByte = endByte;
        this.threadNum = threadNum;
        this.outputFile = outputFile;
    }



    @Override
    public void run() {
        download();
    }

    private void download(){
        try {

            System.out.printf("Thread %d is working...\n" , threadNum);

            HttpURLConnection httpURLConnection = (HttpURLConnection) downloadURL.openConnection();

            httpURLConnection.setRequestProperty("Range", "bytes="+startByte+"-"+endByte);

            httpURLConnection.connect();

            outputFile.seek(startByte);

            stream = httpURLConnection.getInputStream();

            while(true){                                                                
                int nextByte = stream.read();
                if(nextByte == -1){
                    break;
                }

                outputFile.write(endByte);

            }

        } catch (IOException ex) {
            Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }
}

This file is MP4 for as you seen, but Gom can't play it Would you please help me?

Community
  • 1
  • 1
Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51

1 Answers1

0

OoOoOopppps finally I found what is the problem , It's all on seek method. because i have a file and 8 threads. so seek method changes the cursor repeatedly and make larger file and unexecutable file :), But I'm so sorry . I can't show whole code :)

Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51