1. 程式人生 > >Android 原始碼 URLUTIL 判斷檔案型別問題

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; }

問題最終指向了

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 替換。