下载管理器 java 使用多线程

Download Manager In java using multi threading

正如您在我在 java 中开发下载管理器之前看到的那样,我已经问过 and I have read This Question 但这些并没有解决我的问题。现在我在 java 中写了另一个代码。但有一个问题。下载完成后文件过大,相关软件无法读取

这是我的代码执行的图像:

如您所见,文件大小约为 9.43 MB

这是我的项目目录的图像:

如您所见,我下载的文件大小约为 13 MB

那么我的问题是什么?

这是我的完整源代码

主要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);

    }
}

下载信息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);
        }

    }

}

这是我的下载器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);
        }   
    }
}

如您所见,此文件为 MP4,但 Gom 无法播放 你能帮帮我吗?

OoOoOopppps 终于找到问题所在了,都是seek方法。因为我有一个文件和 8 个线程。所以 seek 方法反复更改光标并生成更大的文件和无法执行的文件:),但我很抱歉。我无法显示整个代码 :)