Android 源码 URLUTIL 判断文件类型问题

今天修复一个使用系统的方法 android.webkit.URLUtil.guessFileName(String url,String contentDisposition, String mimeType) 方法获取文件名。

传入参数如图:

技术分享

问题来了,guessFileName 返回的文件名不是 contentDisposition 中给出的fileName ,而是 qujing-for-android.bin
看了下URLUtil 的源码

public static final String guessFileName(
            String url,
            String contentDisposition,
            String mimeType) {
        String filename = null;
        String extension = null;

        // If we couldn‘t do anything with the hint, move toward the content disposition
        if (filename == null && contentDisposition != null) {
            filename = parseContentDisposition(contentDisposition);
            if (filename != null) {
                int index = filename.lastIndexOf(‘/‘) + 1;
                if (index > 0) {
                    filename = filename.substring(index);
                }
            }
        }

        // If all the other http-related approaches failed, use the plain uri
        if (filename == null) {
            String decodedUrl = Uri.decode(url);
            if (decodedUrl != null) {
                int queryIndex = decodedUrl.indexOf(‘?‘);
                // If there is a query string strip it, same as desktop browsers
                if (queryIndex > 0) {
                    decodedUrl = decodedUrl.substring(0, queryIndex);
                }
                if (!decodedUrl.endsWith("/")) {
                    int index = decodedUrl.lastIndexOf(‘/‘) + 1;
                    if (index > 0) {
                        filename = decodedUrl.substring(index);
                    }
                }
            }
        }

        // Finally, if couldn‘t get filename from URI, get a generic filename
        if (filename == null) {
            filename = "downloadfile";
        }

        // Split filename between base and extension
        // Add an extension if filename does not have one
        int dotIndex = filename.indexOf(‘.‘);
        if (dotIndex < 0) {
            if (mimeType != null) {
                extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
                if (extension != null) {
                    extension = "." + extension;
                }
            }
            if (extension == null) {
                if (mimeType != null && mimeType.toLowerCase(Locale.ROOT).startsWith("text/")) {
                    if (mimeType.equalsIgnoreCase("text/html")) {
                        extension = ".html";
                    } else {
                        extension = ".txt";
                    }
                } else {
                    extension = ".bin";
                }
            }
        } else {
            if (mimeType != null) {
                // Compare the last segment of the extension against the mime type.
                // If there‘s a mismatch, discard the entire extension.
                int lastDotIndex = filename.lastIndexOf(‘.‘);
                String typeFromExt = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                        filename.substring(lastDotIndex + 1));
                if (typeFromExt != null && !typeFromExt.equalsIgnoreCase(mimeType)) {
                //这里是判断后缀名,有问题
                    extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
                    if (extension != null) {
                        extension = "." + extension;
                    }
                }
            }
            if (extension == null) {
                extension = filename.substring(dotIndex);
            }
            filename = filename.substring(0, dotIndex);
        }

        return filename + extension;
    }

URLUtil源码地址

问题最终指向了

MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);

这个方法,是系统保存的一个MIMETYPE 的HashMap ,根据传入的Key,返回对应的文件类型

  add("application/pgp-signature", "pgp");
  add("application/pics-rules", "prf");
  add("application/pkix-cert", "cer");
  add("application/rar", "rar");
  add("application/rdf+xml", "rdf");
  add("application/rss+xml", "rss");
  add("application/zip", "zip");
  add("application/vnd.android.package-archive", "apk");
  add("application/vnd.cinderella", "cdy");
  add("application/vnd.ms-pki.stl", "stl");

这里列举了一部分。

但是注意了,我们传入的文件类型 mimetype = application/otcstream ,系统中是没有这个的。

The content-type should be whatever it is known to be, if you know it. application/octet-stream is defined as “arbitrary binary data” in RFC 2046, and there’s a definite overlap here of it being appropriate for entities whose sole intended purpose is to be saved to disk, and from that point on be outside of anything “webby”. Or to look at it from another direction; the only thing one can safely do with application/octet-stream is to save it to file and hope someone else knows what it’s for.

You can combine the use of Content-Disposition with other content-types, such as image/png or even text/html to indicate you want saving rather than display. It used to be the case that some browsers would ignore it in the case of text/html but I think this was some long time ago at this point (and I’m going to bed soon so I’m not going to start testing a whole bunch of browsers right now; maybe later).

RFC 2616 also mentions the possibility of extension tokens, and these days most browsers recognise inline to mean you do want the entity displayed if possible (that is, if it’s a type the browser knows how to display, otherwise it’s got no choice in the matter). This is of course the default behaviour anyway, but it means that you can include the filename part of the header, which browsers will use (perhaps with some adjustment so file-extensions match local system norms for the content-type in question, perhaps not) as the suggestion if the user tries to save.

Hence:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename=”picture.png”
Means “I don’t know what the hell this is. Please save it as a file, preferably named picture.png”.

Content-Type: image/png
Content-Disposition: attachment; filename=”picture.png”
Means “This is a PNG image. Please save it as a file, preferably named picture.png”.

Content-Type: image/png
Content-Disposition: inline; filename=”picture.png”
Means “This is a PNG image. Please display it unless you don’t know how to display PNG images. Otherwise, or if the user chooses to save it, we recommend the name picture.png for the file you save it as”.

Of those browsers that recognise inline some would always use it, while others would use it if the user had selected “save link as” but not if they’d selected “save” while viewing (or at least IE used to be like that, it may have changed some years ago).

这是StackOverFlower 上对 该类型的介绍,原地址在这里

最后,说下问题解决方法:

MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType)传入的参数使用 typeFromExt 替换。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。