<span id="Label3"></p><p><p>Fix a method for using the system today <strong>android.webkit.URLUtil.guessFileName (string url,string contentdisposition, string MimeType)</strong> method to get the file Name.</p></p><p><p>Incoming parameters</p></p><p><p></p></p><p><p>The problem is that the file name returned by Guessfilename is not the filename given in contentdisposition, but <strong>qujing-for-android.bin</strong><br>Look at the source of the next Urlutil</p></p><pre class="prettyprint"><code class=" hljs axapta"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>String Guessfilename (string url, string contentdisposition, string MimeType) {St Ring filename =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; String Extension =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-comment"><span class="hljs-comment">//If We couldn ' t do anything with the hint, move toward the content disposition</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(filename = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>&& contentdisposition! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>{filename = Parsecontentdisposition (contentdisposition);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(filename! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Index</span></span>= Filename.lastindexof (<span class="hljs-string"><span class="hljs-string">'/'</span></span>) +<span class="hljs-number"><span class="hljs-number">1</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">Index</span></span>><span class="hljs-number"><span class="hljs-number">0</span></span>) {filename = filename.substring (<span class="hljs-keyword"><span class="hljs-keyword">Index</span></span>); } } }<span class="hljs-comment"><span class="hljs-comment">//If All the other http-related approaches failed, use the plain URI</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(filename = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {String Decodedurl = Uri.decode (url);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(decodedurl! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Queryindex = Decodedurl.indexof (<span class="hljs-string"><span class="hljs-string">'? '</span></span>);<span class="hljs-comment"><span class="hljs-comment">//If There is a query string strip it, same as desktop browsers</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(queryindex ><span class="hljs-number"><span class="hljs-number">0</span></span>) {decodedurl = decodedurl.substring (<span class="hljs-number"><span class="hljs-number">0</span></span>, queryindex); }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(!decodedurl.endswith (<span class="hljs-string"><span class="hljs-string">"/"</span></span>)) {<span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Index</span></span>= Decodedurl.lastindexof (<span class="hljs-string"><span class="hljs-string">'/'</span></span>) +<span class="hljs-number"><span class="hljs-number">1</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">Index</span></span>><span class="hljs-number"><span class="hljs-number">0</span></span>) {filename = decodedurl.substring (<span class="hljs-keyword"><span class="hljs-keyword">Index</span></span>); } } } }<span class="hljs-comment"><span class="hljs-comment">//Finally, If couldn ' t get filename from URI, get a generic filename</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(filename = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {filename =<span class="hljs-string"><span class="hljs-string">"downloadfile"</span></span>; }<span class="hljs-comment"><span class="hljs-comment">//Split filename between base and extension</span></span> <span class="hljs-comment"><span class="hljs-comment">//Add an extension if filename does</span> not having one</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Dotindex = Filename.indexof (<span class="hljs-string"><span class="hljs-string">'. '</span></span>);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(dotindex <<span class="hljs-number"><span class="hljs-number">0</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(mimeType! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {extension = Mimetypemap.getsingleton (). getextensionfrommimetype (mimeType);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(extension! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {extension =<span class="hljs-string"><span class="hljs-string">"."</span></span>+ extension; } }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(extension = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(mimeType! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>&& mimetype.tolowercase (locale.root). StartsWith (<span class="hljs-string"><span class="hljs-string">"text/"</span></span>)) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(mimetype.equalsignorecase (<span class="hljs-string"><span class="hljs-string">"text/html"</span></span>) {extension =<span class="hljs-string"><span class="hljs-string">". html"</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{extension =<span class="hljs-string"><span class="hljs-string">". txt"</span></span>; } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{extension =<span class="hljs-string"><span class="hljs-string">". bin"</span></span>; } } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(mimeType! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-comment"><span class="hljs-comment">//Compare The last segment of the extension against the MIME type.</span></span> <span class="hljs-comment"><span class="hljs-comment">//If there ' s a mismatch, discard the entire Extension.</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Lastdotindex = Filename.lastindexof (<span class="hljs-string"><span class="hljs-string">'. '</span></span>); String typefromext = Mimetypemap.getsingleton (). getmimetypefromextension (filename.substring (lastDo Tindex +<span class="hljs-number"><span class="hljs-number">1</span></span>));<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(typefromext! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>&&!typefromext.equalsignorecase (mimeType)) {<span class="hljs-comment"><span class="hljs-comment">//here is the name of the suffix, there is a problem</span></span>Extension = Mimetypemap.getsingleton (). getextensionfrommimetype (mimeType);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(extension! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {extension =<span class="hljs-string"><span class="hljs-string">"."</span></span>+ extension; } } }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(extension = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {extension = filename.substring (dotindex); } filename = filename.substring (<span class="hljs-number"><span class="hljs-number">0</span></span>, dotindex); }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>FileName + extension; }</code></pre><p><p>Urlutil Source Address</p></p><p><p>The problem finally points to</p></p><p><p>Mimetypemap.getsingleton (). Getextensionfrommimetype (mimeType);</p></p><p><p>This method, which is a mimetype hashmap saved by the system, returns the corresponding file type according to the key passed IN.</p></p><pre class="prettyprint"><code class=" hljs avrasm"> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/pgp-signature"</span></span>,<span class="hljs-string"><span class="hljs-string">"pgp"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/pics-rules"</span></span>,<span class="hljs-string"><span class="hljs-string">"prf"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/pkix-cert"</span></span>,<span class="hljs-string"><span class="hljs-string">"cer"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/rar"</span></span>,<span class="hljs-string"><span class="hljs-string">"rar"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/rdf+xml"</span></span>,<span class="hljs-string"><span class="hljs-string">"rdf"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/rss+xml"</span></span>,<span class="hljs-string"><span class="hljs-string">"rss"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/zip"</span></span>,<span class="hljs-string"><span class="hljs-string">"zip"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/vnd.android.package-archive"</span></span>,<span class="hljs-string"><span class="hljs-string">"apk"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/vnd.cinderella"</span></span>,<span class="hljs-string"><span class="hljs-string">"cdy"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Add</span></span>(<span class="hljs-string"><span class="hljs-string">"application/vnd.ms-pki.stl"</span></span>,<span class="hljs-string"><span class="hljs-string">"stl"</span></span>)<span class="hljs-comment"><span class="hljs-comment">;</span></span></code></pre><p><p>Here is a partial list.</p></p><p><p>But notice that the file type we passed in MimeType = application/otcstream, which is not in the System.</p></p> <blockquote> <p><p>The content-type should be whatever it's known to being, if you know it. Application/octet-stream is defined as "arbitrary binary data" in RFC 2046, and there's a definite overlap Here's it bein G appropriate for entities whose sole intended purpose are to being saved to disk, and from this point on being outside of Anythi Ng "webby". Or to look at the it from another direction; The only thing one can safely does with application/octet-stream are to save it to file and hope someone else knows what it ' s For.</p></p> <p><p>You can combine the use of content-disposition with other content-types, such as image/png or even text/html to indicate y ou want saving rather than display. It used to being the case that some browsers would ignore it in the case of text/html but I think this is some long time ago At the this point (and i ' M going-to-bed soon so I'm not going to start testing a whole bunch of the browsers right now; maybe lat Er).</p></p> <p><p>RFC 2616 also mentions the possibility of extension tokens, and these days most browsers recognise-inline to mean-do W Ant the entity displayed if possible (that's, if it's a type the browser knows how to display, otherwise it's got no Choi Ce in the matter). This is of course the default behaviour anyway, but it means so can include the filename part of the header, which B rowsers'll use (perhaps with some adjustment so file-extensions match Local system norms for the Content-type in Questio n, Perhaps Not) as the suggestion if the user tries to Save.</p></p> <p><p>Hence:</p></p> <p><p>Content-type:application/octet-stream<br>content-disposition:attachment; Filename= "picture.png"<br>Means "I don ' t know what the hell is. Please save it as a file, preferably named Picture.png ".</p></p> <p><p>Content-type:image/png<br>content-disposition:attachment; Filename= "picture.png"<br>Means "this is a PNG image. Please save it as a file, preferably named Picture.png ".</p></p> <p><p>Content-type:image/png<br>content-disposition:inline; Filename= "picture.png"<br>Means "this is a PNG image. Please display the IT unless you don ' t know how to display PNG Images. Otherwise, or if the user chooses to save it, we recommend the name of Picture.png for the file you save it as ".</p></p> <p><p>Of those browsers that recognise inline some would always use it and 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 is like this, it may has changed some Y Ears ago).</p></p> </blockquote><p><p>This is an introduction to the type on stackoverflower, the original address is here</p></p><p><p>finally, The problem solving method:</p></p><p><p><strong>Mimetypemap.getsingleton (). getextensionfrommimetype (mimeType) The parameters passed in using Typefromext Substitution. </strong></p></p> <p><p> Android source Urlutil to determine file type problems </p> </p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service