Java实现的URL编码和解码技术
laolang
8年前
<p style="text-align:center"><img src="https://simg.open-open.com/show/5b30a4fbf34fea245a4751a6a3d22c1b.jpg"></p> <p>当编写 “ 网络爬虫” 或下载器时,在 Java 中实现 URL 编码和解码是一个很常见的要求。本文的重点是创建用于对所传递的 URL 进行编码和解码的模块。</p> <p><strong>Main 方法</strong></p> <pre> <code class="language-java">public static void main(String[] args) { // TODO Auto-generated method stub String url="https%3A%2F%2Fr1---sn-ci5gup-cags.googlevideo.com%2Fvideoplayback%3Fpcm2cms%3Dyes%26mime%3Dvideo%252Fmp4%26pl%3D21%26itag%3D22%26\u0026itag=43\u0026type=video%2Fwebm%3B+codecs%3D%22vp8.0%2C+vorbis%22\u0026quality=medium"; String url2="https://r1---sn-ci5gup-cags.googlevideo.com/videoplayback?pcm2cms=yes&mime=video/mp4&pl=21&itag=22&&itag=43&type=video/webm; codecs=\"vp8.0, vorbis\"&quality=medium"; String decodeURL = decode(url); System.out.println("Decoded URL: "+decodeURL); String encodeURL = encode(url2); System.out.println("Encoded URL2: "+encodeURL); } </code></pre> <p><strong>它是如何工作的?</strong></p> <ul> <li>url 是一个变量,保存着我们希望解码的已被编码的 URL</li> <li>url2 是保存着我们希望编码的 url 的变量</li> <li>调用 decode 方法,该方法解码和打印 URL</li> <li>调用 encode 方法,该方法编码和打印 url2</li> </ul> <p><strong>Encode 方法</strong></p> <pre> <code class="language-java">public static String encode(String url) { try { String encodeURL=URLEncoder.encode( url, "UTF-8" ); return encodeURL; } catch (UnsupportedEncodingException e) { return "Issue while encoding" +e.getMessage(); } } </code></pre> <p><strong>它是如何工作的</strong></p> <ul> <li>使用名为 URLEncoder 的预定义 Java 类的 encode 方法</li> <li>URLEncoder 类的 encode 方法需要两个参数:</li> </ul> <p>第一个参数定义的是待编码的 URL</p> <p>第二个参数定义的是使用的编码方案</p> <ul> <li>编码之后,将返回编码后的 URL 结果</li> </ul> <pre> <code class="language-java">public static String decode(String url) { try { String prevURL=""; String decodeURL=url; while(!prevURL.equals(decodeURL)) { prevURL=decodeURL; decodeURL=URLDecoder.decode( decodeURL, "UTF-8" ); } return decodeURL; } catch (UnsupportedEncodingException e) { return "Issue while decoding" +e.getMessage(); } } </code></pre> <p><strong>Decode 方法</strong></p> <pre> <code class="language-java">public static String decode(String url){ try { String prevURL=""; String decodeURL=url; while(!prevURL.equals(decodeURL)) { prevURL=decodeURL; decodeURL=URLDecoder.decode( decodeURL, "UTF-8" ); } return decodeURL; } catch (UnsupportedEncodingException e) { return "Issue while decoding" +e.getMessage(); } } </code></pre> <p><strong>它是如何工作的</strong></p> <ul> <li>因为相同的 URL 可以被多次编码,所以我们需要一直对它进行解码直到不能再解码为止。举个例子,"video%252Fmp4" 是两次编码的结果。第一次解码后,我们得到 "video%2Fmp4"。要得到正确的结果 "video/mp4",我们需要再解码一次。</li> <li>使用名为 URLEncoder 的预定义 Java 类的 decode 方法来解码</li> <li>URLDecoder 类的 decode 方法需要两个参数:</li> </ul> <p>第一个参数定义需要解码的 URL</p> <p>第二个参数定义使用的解码方案</p> <ul> <li>解码后,返回已解码的 URL.</li> <li>创建两个变量:prevURL 为空串,decodeURL 包含待解码的 URL</li> </ul> <pre> <code class="language-java">Variable State: prevURL = "" decodeURL ="somethingvideo%252Fmp4" </code></pre> <ul> <li>创建一个重复执行的步骤,直到 prevURL 与 decodeURL 的值相等</li> <li>将 decodeURL 的值赋值给 prevURL,将传递的 URL 解码后的值赋给 decodeURL</li> </ul> <pre> <code class="language-java">Variable State: prevURL = "somethingvideo%252Fmp4" decodeURL ="somethingvideo%2Fmp4" </code></pre> <ul> <li>如你所见,prevURL 的值不等于 decodeURL 的值,我们再次执行</li> </ul> <pre> <code class="language-java">Variable State: prevURL = "somethingvideo%2Fmp4" decodeURL ="somethingvideo/mp4" </code></pre> <ul> <li>再一次</li> </ul> <pre> <code class="language-java">Variable State: prevURL = "somethingvideo/mp4" decodeURL ="somethingvideo/mp4" </code></pre> <ul> <li>现在,prevURL 的值等于 decodeURL 的值了,得到了正确的解码结果。</li> </ul> <p><strong>输出</strong></p> <pre> <code class="language-java">Decoded URL: https://r1---sn-ci5gup-cags.googlevideo.com/videoplayback?pcm2cms=yes&mime=video/mp4&pl=21&itag=22&&itag=43&type=video/webm; codecs="vp8.0, vorbis"&quality=medium Encoded URL2: https%3A%2F%2Fr1---sn-ci5gup-cags.googlevideo.com%2Fvideoplayback%3Fpcm2cms%3Dyes%26mime%3Dvideo%2Fmp4%26pl%3D21%26itag%3D22%26%26itag%3D43%26type%3Dvideo%2Fwebm%3B+codecs%3D%22vp8.0%2C+vorbis%22%26quality%3Dmedium </code></pre> <p><strong>完整的程序</strong></p> <pre> <code class="language-java">package com.cooltrickshome; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; public class URLEncodeDecode { public static void main(String[] args) { // TODO Auto-generated method stub String url="https%3A%2F%2Fr1---sn-ci5gup-cags.googlevideo.com%2Fvideoplayback%3Fpcm2cms%3Dyes%26mime%3Dvideo%252Fmp4%26pl%3D21%26itag%3D22%26\u0026itag=43\u0026type=video%2Fwebm%3B+codecs%3D%22vp8.0%2C+vorbis%22\u0026quality=medium"; String url2="https://r1---sn-ci5gup-cags.googlevideo.com/videoplayback?pcm2cms=yes&mime=video/mp4&pl=21&itag=22&&itag=43&type=video/webm; codecs=\"vp8.0, vorbis\"&quality=medium"; String decodeURL = decode(url); System.out.println("Decoded URL: "+decodeURL); String encodeURL = encode(url2); System.out.println("Encoded URL2: "+encodeURL); } public static String decode(String url) { try { String prevURL=""; String decodeURL=url; while(!prevURL.equals(decodeURL)) { prevURL=decodeURL; decodeURL=URLDecoder.decode( decodeURL, "UTF-8" ); } return decodeURL; } catch (UnsupportedEncodingException e) { return "Issue while decoding" +e.getMessage(); } } public static String encode(String url) { try { String encodeURL=URLEncoder.encode( url, "UTF-8" ); return encodeURL; } catch (UnsupportedEncodingException e) { return "Issue while encoding" +e.getMessage(); } } } </code></pre> <p> </p> <p>来自:http://developer.51cto.com/art/201704/536354.htm</p> <p> </p>