我对这个奇怪的问题感到震惊。
我正在使用以下方法下载文件:
public boolean downloadSection(String link,String fileLocation,long currentlyDownloadedBytes){
try {
RandomAccessFile file = new RandomAccessFile(fileLocation, "rw");
file.seek(currentlyDownloadedBytes+1);
URL imageUrl = new URL(link);
HttpURLConnection conn =(HttpURLConnection)imageUrl.openConnection();
conn.setRequestProperty("Range", "bytes=" + currentlyDownloadedBytes + "-");
conn.setConnectTimeout(100000);
conn.setReadTimeout(100000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
byte buffer[]=new byte[1024];;
while (true) {
// Read from the server into the buffer
int read = is.read(buffer);
if (read == -1) {
break;
}
// Write buffer to file
file.write(buffer, 0, read);
}
file.close();
return true;
} catch (Exception ex){
ex.printStackTrace();
return false;
}
}
当我使用此代码下载 mp3 文件时,下载文件打开得很好,但是下载的 android 应用程序(.apk 文件)在打开时会出现包解析错误,并且图像永远不会打开。
请帮忙谢谢