Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
下载的字节数略高于文件的总字节数-Java_Java_Download_Urlconnection_Bufferedinputstream - Fatal编程技术网

下载的字节数略高于文件的总字节数-Java

下载的字节数略高于文件的总字节数-Java,java,download,urlconnection,bufferedinputstream,Java,Download,Urlconnection,Bufferedinputstream,嗯,我正在开发一个应用程序,它将读取用户输入的URL,并使用openConnection()方法等下载特定文件。我有一个功能,用户可以选择远程连接的数量。我做了两次测试。第一个测试为1个连接,第二个测试为4个连接。问题在于1连接下载的字节数与文件的总字节数相同,但当我尝试4连接时,下载的字节数总是大于总字节数。因此,当我打开下载的文件时,它看起来已损坏。这是我的密码: 以下是来自DownloadFile.java SubDownload sd[]; int DownloadID=1, Activ

嗯,我正在开发一个应用程序,它将读取用户输入的URL,并使用
openConnection()
方法等下载特定文件。我有一个功能,用户可以选择远程连接的数量。我做了两次测试。第一个测试为1个连接,第二个测试为4个连接。问题在于1连接下载的字节数与文件的总字节数相同,但当我尝试4连接时,下载的字节数总是大于总字节数。因此,当我打开下载的文件时,它看起来已损坏。这是我的密码:

以下是来自
DownloadFile.java

SubDownload sd[];
int DownloadID=1, ActiveSubConn=0, BufferSize=1;
long FileSize = 9301;    //bytes
int TotConnections=1;    //by Default '1' for first testing and '4' for second testing
long ld_FStartPos, ld_FEndPos, ld_partsize;
String partname;
String FileLoc = "http://static.webshopapp.com/shops/010317/files/002503512/140x140x2/13-m-kite-refit-service.jpg";

public int StartDownload()
{ 
    sd = new SubDownload[TotConnections];
    ld_partsize= (long)(FileSize/TotConnections);
            System.out.println("FileSize: "+FileSize);

    for (li_conn=0;li_conn < TotConnections ;li_conn++)
    {
        if ( li_conn == (TotConnections - 1))
        {
            ld_FStartPos=li_conn*ld_partsize;
            ld_FEndPos= FileSize;
        }
        else
        {
            ld_FStartPos=li_conn*ld_partsize;
            ld_FEndPos= ld_FStartPos + ld_partsize - 1;
        }

        partname = "DFL" +  String.valueOf(DownloadID) + String.valueOf(li_conn) + ".txt";
        sd[li_conn] = new SubDownload(partname, FileLoc, ld_FStartPos, ld_FEndPos, BufferSize);
        sd[li_conn].start();
        ActiveSubConn = ActiveSubConn + 1;
    }
    return li_conn;          
}
public  String SubDownloadId, FileLoc;
public  long FileStartPos, FileEndPos, BytesDownloaded=0;
public  byte Buffer[];

public SubDownload(String aSubDownloadId,String aFileLoc,long aFileStartPos,
                long aFileEndPos,int aBufferSize){

    FileLoc=aFileLoc;
    FileStartPos=aFileStartPos;
    FileEndPos=aFileEndPos;
    Buffer = new byte[1024 * aBufferSize];  
    BytesDownloaded=0;
    SubDownloadId=aSubDownloadId;
}

public void run(){
    try{
        URL url = new URL(FileLoc);
        URLConnection uc = url.openConnection();
        BufferedInputStream instream;
        uc.setRequestProperty("Range","bytes=" + FileStartPos + "-"+ FileEndPos);

        instream = new BufferedInputStream(uc.getInputStream());

        int li_bytesRead;
        File f = new File(SubDownloadId);
        RandomAccessFile raf = new RandomAccessFile(f, "rw");

        while(BytesDownloaded < (FileEndPos - FileStartPos))
        {
            if (status ==1)
            {
                if(BytesDownloaded==0) System.out.println("Start: "+FileStartPos+" End: "+FileEndPos);
                li_bytesRead = instream.read(Buffer);
                raf.write(Buffer,0,li_bytesRead);
                BytesDownloaded = BytesDownloaded + li_bytesRead;
                System.out.println("Bytes Read: "+li_bytesRead+" TotalBytesRead: "+BytesDownloaded);
             }
        }

    }catch(Exception e){}


    }
现在是测试部分。在第一次测试中,我将
TotConnections=1
并开始下载。这是控制台中打印的结果

FileSize:9301
起点:0终点:9301
读取字节数:1024总计字节数读取:1024
读取字节数:1024总字节数读取:2048
读取字节数:1024总字节数读取:3072
读取字节数:1024总字节数读取:4096
读取字节数:1024总字节数读取:5120
读取字节数:1024总字节数读取:6144
读取字节数:1024总字节数读取:7168
读取字节数:1024总字节数读取字节数:8192
读取字节数:1024总字节数读取:9216
字节读取:85个字节读取:9301

当我打开下载的文件时,它已成功打开,没有出现错误或损坏。接下来在第二次测试中,我设置
TotConnections=4并启动。结果是:

FileSize:9301
起点:4650终点:6974
读取字节数:1024总计字节数读取:1024
字节读取:22个字节读取:1046
读取字节数:1024总字节数读取:2070
读取字节数:1024总字节数读取:3094
起点:6975终点:9301
读取字节数:1024总计字节数读取:1024
读取字节数:1024总字节数读取:2048
读取字节数:1024总字节数读取:3072
开始:0结束:2324
读取字节数:1024总计字节数读取:1024
读取字节数:1024总字节数读取:2048
读取字节数:1024总字节数读取:3072
起点:2325终点:4649
读取字节数:1024总计字节数读取:1024
读取字节数:1024总字节数读取:2048
字节读取:1024总字节读取:3072

如果现在我打开下载的文件,它将无法正常打开。文件已损坏。总之,第一次测试中的
TotalBytesRead
9301
字节,而第二次测试为
12310
字节

如果我将TotConnections设置为1,则没有任何问题,但1除外。有人能告诉我我犯了什么错误吗?请给我一个解决办法

**更正:只有在某些情况下,使用4个或以上连接下载的文件才不会被损坏,否则大多数情况下,文件只会被损坏