好吧,我正在开发一个应用程序,它将读取用户输入的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;
}
以下是来自SubDownload.java
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
Start: 0 End: 9301
Bytes Read: 1024 TotalBytesRead: 1024
Bytes Read: 1024 TotalBytesRead: 2048
Bytes Read: 1024 TotalBytesRead: 3072
Bytes Read: 1024 TotalBytesRead: 4096
Bytes Read: 1024 TotalBytesRead: 5120
Bytes Read: 1024 TotalBytesRead: 6144
Bytes Read: 1024 TotalBytesRead: 7168
Bytes Read: 1024 TotalBytesRead: 8192
Bytes Read: 1024 TotalBytesRead: 9216
Bytes Read: 85 TotalBytesRead: 9301
当我打开下载的文件时,它已成功打开,没有发生错误或损坏。接下来在第二次测试中我设置了TotConnections=4;
并开始了。结果:
FileSize: 9301
Start: 4650 End: 6974
Bytes Read: 1024 TotalBytesRead: 1024
Bytes Read: 22 TotalBytesRead: 1046
Bytes Read: 1024 TotalBytesRead: 2070
Bytes Read: 1024 TotalBytesRead: 3094
Start: 6975 End: 9301
Bytes Read: 1024 TotalBytesRead: 1024
Bytes Read: 1024 TotalBytesRead: 2048
Bytes Read: 1024 TotalBytesRead: 3072
Start: 0 End: 2324
Bytes Read: 1024 TotalBytesRead: 1024
Bytes Read: 1024 TotalBytesRead: 2048
Bytes Read: 1024 TotalBytesRead: 3072
Start: 2325 End: 4649
Bytes Read: 1024 TotalBytesRead: 1024
Bytes Read: 1024 TotalBytesRead: 2048
Bytes Read: 1024 TotalBytesRead: 3072
如果现在我打开下载的文件,它将无法正常打开。该文件已损坏。总之,第一次测试中的TotalBytesRead
为9301
字节,而第二次测试中为12310
字节。
如果我将TotConnections设置为1,则除了1之外没有任何问题导致问题。有人能告诉我,我做的错误是什么?请给我一个解决方案。
**更正:有时只下载使用4个或更多1个连接下载的文件而不会损坏,否则大多数情况下文件仅被破坏。