Android 如何在下载文件之前知道文件的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2983073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:10:16  来源:igfitidea点击:

How to know the size of a file before downloading it?

androiddownload

提问by Cristian

I have to download a file and I'm using this code, which is basically an AsyncTaskthat is meant to update a progress bar. But, since I don't know what's the file size I've been having to use the spinner progress bar. So, how can I get the file size before start downloading it so that I can use a normal progress bar?

我必须下载一个文件并且我正在使用这个代码,它基本上是一个AsyncTask用于更新进度条的代码。但是,由于我不知道文件大小是多少,我不得不使用微调进度条。那么,如何在开始下载之前获取文件大小以便我可以使用正常的进度条?

回答by reflog

you can get a header called Content-Lengthform the HTTP Response object that you get, this will give you the length of the file. you should note though, that some servers don't return that information, and the only way to know the actual size is to read everything from the response.

您可以从您获得Content-Length的 HTTP 响应对象中获得一个名为的标头,这将为您提供文件的长度。不过您应该注意,有些服务器不会返回该信息,而了解实际大小的唯一方法是从响应中读取所有内容。

Example:

例子:

URL url = new URL("http://server.com/file.mp3");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();

回答by android developer

you can usually use getContentLength, but the best thing is it get the length by yourself (since it can bypass integer's max value) .

您通常可以使用getContentLength,但最好的是它自己获取长度(因为它可以绕过整数的最大值)。

just parse the content-length header value by yourself . better parse it as long .

只需自己解析 content-length 标头值。更好地解析它。

example:

例子:

final URL uri=new URL(...);
URLConnection ucon;
try
  {
  ucon=uri.openConnection();
  ucon.connect();
  final String contentLengthStr=ucon.getHeaderField("content-length");
  //...
  }
catch(final IOException e1)
  {
  }

do note that i can be any string , so use try catch , and if it's -1, empty , or null , it means that you can't know the size of the file since the server doesn't allow it.

请注意, i 可以是任何字符串,因此请使用 try catch ,如果它是 -1、empty 或 null ,则意味着您无法知道文件的大小,因为服务器不允许。

EDIT: Here's a more updated code, using Kotlin:

编辑:这是一个更新的代码,使用 Kotlin:

@JvmStatic
@WorkerThread
fun getFileSizeOfUrl(url: String): Long {
    var urlConnection: URLConnection? = null
    try {
        val uri = URL(url)
        urlConnection = uri.openConnection()
        urlConnection!!.connect()
        if (VERSION.SDK_INT >= Build.VERSION_CODES.N)
            return urlConnection.contentLengthLong
        val contentLengthStr = urlConnection.getHeaderField("content-length")
        return if (contentLengthStr.isNullOrEmpty()) -1 else contentLengthStr.toLong()
    } catch (ignored: Exception) {
    } finally {
        if (urlConnection is HttpURLConnection)
            urlConnection.disconnect()
    }
    return -1
}