当编写 “ 网络爬虫” 或下载器时,在 Java 中实现 URL 编码和解码是一个很常见的要求。本文的重点是创建用于对所传递的 URL 进行编码和解码的模块。你可以看一下 GitHub 上的 源码。
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%26u0026itag=43u0026type=video%2Fwebm%3B+codecs%3D%22vp8.0%2C+vorbis%22u0026quality=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); }
url 是一个变量,保存着我们希望解码的已被编码的 URL
url2 是保存着我们希望编码的 url 的变量
调用 decode 方法,该方法解码和打印 URL
调用 encode 方法,该方法编码和打印 url2
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(); } }
使用名为 URLEncoder 的预定义 Java 类的 encode 方法
URLEncoder 类的 encode 方法需要两个参数:
第一个参数定义的是待编码的 URL
第二个参数定义的是使用的编码方案
编码之后,将返回编码后的 URL 结果
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(); } }其它翻译版本 (1) 加载中
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(); } }
因为相同的 URL 可以被多次编码,所以我们需要一直对它进行解码直到不能再解码为止。举个例子,"video%252Fmp4" 是两次编码的结果。第一次解码后,我们得到 "video%2Fmp4"。要得到正确的结果 "video/mp4",我们需要再解码一次。
使用名为 URLEncoder 的预定义 Java 类的 decode 方法来解码
URLDecoder 类的 decode 方法需要两个参数:
第一个参数定义需要解码的 URL
第二个参数定义使用的解码方案
解码后,返回已解码的 URL.
创建两个变量:prevURL 为空串,decodeURL 包含待解码的 URL
Variable State: prevURL = "" decodeURL ="somethingvideo%252Fmp4"
6. 创建一个重复执行的步骤,直到 prevURL 与 decodeURL 的值相等
7. 将 decodeURL 的值赋值给 prevURL,将传递的 URL 解码后的值赋给 decodeURL
Variable State: prevURL = "somethingvideo%252Fmp4" decodeURL ="somethingvideo%2Fmp4"
8. 如你所见,prevURL 的值不等于 decodeURL 的值,我们再次执行
Variable State: prevURL = "somethingvideo%2Fmp4" decodeURL ="somethingvideo/mp4"
9. 再一次
Variable State: prevURL = "somethingvideo/mp4" decodeURL ="somethingvideo/mp4"
10. 现在,prevURL 的值等于 decodeURL 的值了,得到了正确的解码结果。
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
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%26u0026itag=43u0026type=video%2Fwebm%3B+codecs%3D%22vp8.0%2C+vorbis%22u0026quality=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(); } } }
希望能帮到你!
其它翻译版本 (2) 加载中 本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。 2KB翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。2KB项目(www.2kb.com,源码交易平台),提供担保交易、源码交易、虚拟商品、在家创业、在线创业、任务交易、网站设计、软件设计、网络兼职、站长交易、域名交易、链接买卖、网站交易、广告买卖、站长培训、建站美工等服务