在Android上下载文件时未检测到0字节文件

时间:2013-07-01 08:11:41

标签: android file download size inputstream

我有一个Android应用程序,它可以从互联网上下载数百个文件。一些文件在下载后变为0字节。该应用程序尝试检测此类情况并在下载后删除此类文件,但有时会失败。 Android 4.x设备上的问题更频繁。

以下是进行下载的方法。我从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字节。它们无法由视频播放器播放(显示为“损坏的文件”图标)。

2 个答案:

答案 0 :(得分:1)

您应该在FileOutputStream上调用flush()以确保将所有数据写入文件。这应该会使您的0字节文件出现问题的频率降低。

使用File.length()检查0字节文件应该正常工作。你可以在设备上打开一个shell(adb shell)并运行ls -l来查看它显示的字节数是0(也许你的文件管理器有一些奇怪的问题)。另请调试(或放置一些日志语句)sFullPath包含正确的文件路径。我无法在上面的代码中看到sFullPath的设置,以及为什么不只是使用outputFile而是重新创建另一个File对象。

答案 1 :(得分:1)

在我看来,问题是如果Utils.downloadFile调用返回true,则只检查“垃圾”文件。如果在getInputStream调用或第一个read中下载失败,您将创建一个长度为零的文件,永远不会删除。