使用OkHttp下载损坏的文件

5
我编写的下载文件方法总是会产生损坏的文件。
public static String okDownloadToFileSync(final String link, final String fileName, final boolean temp, DownloadStatusManager statusManager, ErrorDisplayerInterface errorDisplayerInterface) {

    Request request = new Request.Builder()
            .url(link)
            .build();


    OkHttpClient client = Api.getInstance().getOkHttpClient();
    OutputStream output = null;
    InputStream input = null;

    try {

        Response response = client.newCall(request).execute();

        //Add the file length to the statusManager
        final int contentLength = Integer.parseInt(response.header("Content-Length"));
        if (statusManager != null) {
            statusManager.add(Hash.md5(link), contentLength);
        }

        //Get content type to know extension
        final String contentType = response.header("Content-Type");
        final String ext = contentTypeMap.get(contentType);

        Log.i(TAG, link + "\n --> contentType = " + contentType + "\n --> ext = " + ext);

        if (ext == null) {
            Log.e(TAG, "-----------\next is null, seems like there is a problem with that url : \n         " + link + "\n----------");
            return null;
        } else if (ext.equals("json")) {
            Log.e(TAG, "-----------\ndownloadable file seems to be a json, seems like there is a problem with that url : \n         " + link + "\n----------");
            return null;
        }

        //Check if file already exists
        if (!temp && fileName != null) {
            File test = new File(M360Application.getContext().getFilesDir(), fileName + "." + ext);
            if (test.exists()) {
                Log.i(TAG, "File exists ! : " + test.getPath());
                test.delete();
                //return test.getAbsolutePath();
            }
        }

        // expect HTTP 200 OK, so we don't mistakenly save error report instead of the file
        if (!response.isSuccessful()) {
            errorDisplayerInterface.popWarn(null, "Error while downloading " + link, "connection.getResponseCode() != HttpURLConnection.HTTP_OK");
            return null;
        }

        input = response.body().byteStream();

        File file;
        if (temp) {
            file = File.createTempFile(UUID.randomUUID().toString(), ext, M360Application.getContext().getCacheDir());
        } else {
            file = new File(M360Application.getContext().getFilesDir(), fileName + "." + ext);
        }


        output = new FileOutputStream(file);
        
        output.write(response.body().bytes());

//            byte data[] = new byte[4096];
//            long total = 0;
//            int count;
//            while ((count = input.read(data)) != -1) {
//                output.write(data, 0, count);
//                total++;
//
//                if (statusManager != null) {
//                    statusManager.update(Hash.md5(link), contentLength - total);
//                }
//           }

        return file.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        errorDisplayerInterface.popError(null, e);

    } finally {
        if (statusManager != null) {
            statusManager.finish(Hash.md5(link));
        }
        try {
            if (output != null)
                output.close();
            if (input != null)
                input.close();
        } catch (IOException ignored) {
            ignored.printStackTrace();
        }

    }
    return null;
}

我通过adb访问这些文件,将它们传输到我的sccard上,在那里我发现它们似乎具有适当的大小,但是根据例如Linux中的file命令,它们没有类型。

您知道缺少什么以及如何解决吗?

谢谢。


编辑

代码的简化版本(但错误相同)

public static String okioDownloadToFileSync(final String link, final String fileName) throws IOException {

    Request request = new Request.Builder()
            .url(link)
            .build();


    OkHttpClient client = Api.getInstance().getOkHttpClient();
    Response response = client.newCall(request).execute();

    final int contentLength = Integer.parseInt(response.header("Content-Length"));

    //Get content type to know extension
    final String contentType = response.header("Content-Type");
    final String ext = contentTypeMap.get(contentType);

    // expect HTTP 200 OK, so we don't mistakenly save error report instead of the file
    if (!response.isSuccessful()) {
        return null;
    }

    File file = new File(M360Application.getContext().getFilesDir(), fileName + "." + ext);

    BufferedSink sink = Okio.buffer(Okio.sink(file));
    sink.writeAll(response.body().source());
    sink.close();

    Log.i(TAG, "file.length : " + file.length() + " | contentLength : " + contentLength);

    return file.getAbsolutePath();

}

日志: file.length : 2485394 | contentLength : 1399242


解决方案

问题在于我从API单例中获取了OkHttpClient,该单例被Retrofit使用,并且有多个拦截器。这些拦截器会污染响应。

所以我把OkHttpClient client = Api.getInstance().getOkHttpClient();变成了OkHttpClient client = new OkHttpClient.Builder().build();,现在一切都好了!

非常感谢您的帮助。我现在正在将该方法分成更小的部分。


1
他们似乎有正确的大小。看起来?你甚至不知道大小是否相等?到最后一个字节? - greenapps
我添加了一个更简单的代码版本来重现这个错误。我不应该发布我的完整代码。 - Renaud Favier
如果您有Windows电脑,请在Wordpad中打开原始文件和下载的文件。有哪些可见的差异?您正在下载什么类型的文件?是JPG吗?那么尝试使用.txt文件,因为您可以逐行检查txt文件并阅读它们。 - greenapps
哎呀!这是个糟糕的错误:2485394。可能是因为我的 OkHttpClient 有拦截器,我尝试获取一个新的而不是 Retrofit 使用的那个。 - Renaud Favier
就是这样!我用没有拦截器的新OkHttpClient替换了旧的,现在大小正确了!感谢大家在调试过程中的指导!午餐后我会更新我的帖子并附上答案,尽管我怀疑它对其他人有用。 - Renaud Favier
显示剩余9条评论
1个回答

1

不要使用output.write(response.body().bytes());,尝试使用以下代码:

byte[] buff = new byte[1024 * 4];

while (true) {
   int byteCount = input.read(buff);
   if (byteCount == -1) {
       break;
   }
   output.write(buff, 0, byteCount);
}

它看起来很像我之前尝试的被注释掉的部分(就在“output.write(response.body().bytes());”下面)。但我也没有更多的成功经验。 - Renaud Favier
你的日志输出显示了什么?也许,你的内容类型和扩展名有些问题? - Chupik
扩展名没问题,但文件大小不正确:Log.i(TAG, "file.length : " + file.length() + " | contentLength : " + contentLength); => file.length : 2485394 | contentLength : 1399242 - Renaud Favier
请尝试这个建议并报告大小。 - greenapps
从零到字节数,救了我的命 XD - Arif Nouman Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接