当编写 “ 网络爬虫” 或下载器时,在 Java 中实现 URL 编码和解码是一个很常见的要求。本文的重点是创建用于对所传递的 URL 进行编码和解码的模块。你可以看一下 GitHub 上的 源码。 Main 方法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);
} 它是如何工作的?
Encode 方法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();
}
} 它是如何工作的
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();
}
} Decode 方法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();
}
} 它是如何工作的
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%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();
}
}
} 希望能帮到你! |