使用多线程在java中下载管理器

使用多线程在java中下载管理器,java,multithreading,download,Java,Multithreading,Download,正如您之前看到的,我正在用java开发下载管理器,我已经问过了,也读过了,但这些并没有解决我的问题。现在我已经用java编写了另一个代码。但有一个问题。下载完成后,文件大于其大小,相关软件无法读取 这是我的代码执行的图像: 如您所见,文件大小约为9.43MB 这是我的项目目录的映像: 如您所见,我下载的文件大小约为13MB 那么我的问题是什么 这是我的完整源代码 主要类别: package download.manager; import java.util.Scanner; /** *

正如您之前看到的,我正在用java开发下载管理器,我已经问过了,也读过了,但这些并没有解决我的问题。现在我已经用java编写了另一个代码。但有一个问题。下载完成后,文件大于其大小,相关软件无法读取

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

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

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

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

那么我的问题是什么

这是我的完整源代码

主要类别:

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

    }
}
下载信息类:

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

    }

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

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

似乎是outputFile.writeendByte;需要为outputFile.writenextByte@alex2410啊,我犯了个错误,但在更正后没有任何更改。所以在下载中,您似乎读取了从起始字节到文件结尾的所有字节,而不是每个线程的结束字节。@alex2410我现在检查了它,这是真正的范围设置
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);
        }   
    }
}