我有一款適用於Android的應用程序,可從互聯網下載數百個文件。下載後一些文件變爲0字節。該應用程序嘗試檢測這些情況並在下載後刪除這些文件,但有時會失敗。這個問題在Android 4.x設備上更頻繁。在Android上下載文件時未檢測到0字節文件
這裏是下載的方法。我從inputStream.read(buffer)
獲得實際讀取的字節數。
public class Utils
{
public static class DownloadFileData
{
int nTotalSize;
int nDownloadedSize;
}
public interface ProgressCallback
{
void onProgress(long nCurrent, long nMax);
}
public static boolean downloadFile(String sFileURL, File whereToSave, DownloadFileData fileData, ProgressCallback progressCallback)
{
InputStream inputStream = null;
FileOutputStream fileOutput = null;
try
{
URL url = new URL(sFileURL);
URLConnection connection = url.openConnection();
//set up some things on the connection
connection.setDoOutput(true);
connection.connect();
fileOutput = new FileOutputStream(whereToSave);
inputStream = connection.getInputStream();
fileData.nTotalSize = connection.getContentLength();
fileData.nDownloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
// now, read through the input buffer and write the contents to the file
while ((bufferLength = inputStream.read(buffer)) > 0)
{
// if interrupted, don't download the file further and return
// also restore the interrupted flag so that the caller stopped also
if (Thread.interrupted())
{
Thread.currentThread().interrupt();
return false;
}
// add the data in the buffer to the file in the file output stream
fileOutput.write(buffer, 0, bufferLength);
// add up the size so we know how much is downloaded
fileData.nDownloadedSize += bufferLength;
if (null != progressCallback && fileData.nTotalSize > 0)
{
progressCallback.onProgress(fileData.nDownloadedSize, fileData.nTotalSize);
}
}
return true;
}
catch (FileNotFoundException e)
{
return false; // swallow a 404
}
catch (IOException e)
{
return false; // swallow a 404
}
catch (Throwable e)
{
return false;
}
finally
{
// in any case close input and output streams
if (null != inputStream)
{
try
{
inputStream.close();
inputStream = null;
}
catch (Exception e)
{
}
}
if (null != fileOutput)
{
try
{
fileOutput.close();
fileOutput = null;
}
catch (Exception e)
{
}
}
}
}
這是處理下載的代碼片段。由於有時讀取字節的數量不正確(它是> 0,真實文件的大小爲0字節),我用outputFile.length()
檢查下載文件的大小。但是即使文件真的是0字節,這也會給出> 0的值。我試圖創建一個新文件並使用recheckSizeFile.length()
來讀取它的大小。仍然大小確定爲> 0,而它確實是0字節。
Utils.DownloadFileData fileData = new Utils.DownloadFileData();
boolean bDownloadedSuccessully = Utils.downloadFile(app.sCurrenltyDownloadedFile, outputFile, fileData, new Utils.ProgressCallback()
{
... // progress bar is updated here
});
if (bDownloadedSuccessully)
{
boolean bIsGarbage = false;
File recheckSizeFile = new File(sFullPath);
long nDownloadedFileSize = Math.min(recheckSizeFile.length(), Math.min(outputFile.length(), fileData.nDownloadedSize));
// if the file is 0bytes, it's garbage
if (0 == nDownloadedFileSize)
{
bIsGarbage = true;
}
// if this is a video and if of suspiciously small size, it's
// garbage, too
else if (Utils.isStringEndingWith(app.sCurrenltyDownloadedFile, App.VIDEO_FILE_EXTENSIONS) && nDownloadedFileSize < Constants.MIN_NON_GARBAGE_VIDEO_FILE_SIZE)
{
bIsGarbage = true;
}
if (bIsGarbage)
{
++app.nFilesGarbage;
app.updateLastMessageInDownloadLog("File is fake, deleting: " + app.sCurrenltyDownloadedFile);
// delete the garbage file
if (null != outputFile)
{
if (!outputFile.delete())
{
Log.e("MyService", "Failed to delete garbage file " + app.sCurrenltyDownloadedFile);
}
}
}
else
{
... // process the normally downloaded file
}
我不確定,但我認爲在Android中有一個讀取文件大小的錯誤。有沒有人看過類似的問題?或者我可能在這裏做錯了什麼? 謝謝!
編輯:我如何確定文件是0字節: 所有下載的文件都通過上述例程。當我後來使用文件瀏覽器(Ghost Commander)查看下載文件夾時,某些文件(如10%)爲0字節。它們不能由視頻播放器播放(顯示爲「破損文件」圖標)。
文件真的可以是0字節嗎?難道它不會總是有一些元數據等,爲自己存在?也許你需要更多的東西像實際的內容大小? –
其實我也檢查小視頻文件刪除它們。 MIN_NON_GARBAGE_VIDEO_FILE_SIZE設置爲100kb。所以,如果一個視頻文件會有一些小的元數據塊,它肯定會在100kb以下。而且我仍然得到0字節的視頻文件不被刪除。單獨的元數據可以超過100kb嗎? – iseeall