Android file download, get the real file name and suffix name (including redirected url) according to url

When downloading Android files, sometimes we need to get the name and suffix of the file to be downloaded according to the url.

There are two types of urls,

One is a static url: that is, the file name follows the url, for example: https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk  (qq) At this time, we can directly intercept the last '/' and then Get the file and suffix.

But another kind of url is the interface address. The corresponding cloud program will respond and return the real file URL. The redirected URL will contain the file name and suffix.

For this kind of redirected url, how do we get the file name and suffix. There are many ways to check online, but after testing, many of them cannot get the real file name and suffix. In fact, here is the processing of redirection. This problem can be transformed into how to get the real url after redirection. It seems that it is much more accurate to check in this way.

The network framework of our project uses okhttp, so I only wrote the method to get the real url through okhttp, which is actually very simple, because okhttpp has already handled the redirection for us:

 public String getFileName(String url) {
        String fileName = null;
        if (!TextUtils.isEmpty(url)) {
            try {
                OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
                Request request = new Request.Builder()
                    .url(url)//请求接口。如果需要传参拼接到接口后面。
                    .build();//创建Request 对象
                Response response = client.newCall(request).execute();//得到Response 对象
                HttpUrl realUrl = response.request().url();
                Log.e("zmm", "real:" + realUrl);
                if (realUrl != null) {
                    String temp = realUrl.toString();
                    fileName = temp.substring(temp.lastIndexOf("/") + 1);

                }
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("zmm", "Get File Name:error" + e);
            }
        }
        Log.e("zmm", "fileName--->" + fileName);
        return fileName;
    }

The personal test is effective, and I don’t know much about redirection, probably: the processing of redirection relies on recursion until the real address does not change, and the final download address is obtained. Specifically about the redirection processing of okhttp, you can Baidu yourself. I read this article at the beginning: https://segmentfault.com/a/1190000008616578 The explanation is still very detailed.

Daily records:

Many people say that they are lonely, and those who say they are lonely are not lonely. Loneliness is not neglect and abandonment, but ignorance and incomprehension. Lonely people do not talk about loneliness, and occasionally make loud and long howls, just like the beasts we see.

If you live upstairs, you live on the top floor. You live high and look far away. When you watch a movie, you sit in the back row. You can't see the movie clearly, but you can see the audience clearly.

--- "Walking Alone" by Jia Pingwa

Single cycle "appear and leave"

Finally: Yay! Mid-Autumn Festival is coming soon! ! !

Guess you like

Origin blog.csdn.net/androidzmm/article/details/100338521