Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

URL Encoding and Decoding Using Java

DZone's Guide to

URL Encoding and Decoding Using Java

If you find yourself encoding and decoding URLs often, take a look at how to do it in Java while staying on alert in case you need multiple iterations.

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

It is a common requirement to implement URL encoding and decoding in Java while creating crawlers or downloaders. This post focuses on creating modules for encoding and decoding of a passed URL. You can take a look at the source code on GitHub.

Main Method

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


How It Works

  1. url is the variable containing the encoded URL that we want to decode.

  2. url2 is the variable containing the URL we want to encode.

  3. We call the decode method, which decodes and prints the URL.

  4. We call the encode method, which encodes and prints url2.

Encode Method

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


How It Works

  1. We use the encode method of a predefined Java class named URLEncoder.

  2. The encode method of URLEncoder takes two arguments:

    1. The first argument defines the URL to be encoded.

    2. The second argument defines the encoding scheme to be used.

  3. After encoding, the resulting encoded URL is returned.

Decode Method

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


How It Works

  1. Because the same URL can be encoded multiple times, we need to decode it until the URL cannnot be decoded further. For example, "video%252Fmp4" is the result of two encodings. Upon decoding it once, we get "video%2Fmp4". Now the URL needs to be further decoded so that we get "video/mp4", which is the result.

  2. We use the decode method of a predefined Java class named URLDecoder.

  3. The decode method of URLDecoder takes two arguments:

    1. The first argument defines the URL to be decoded.

    2. The second argument defines the decoding scheme to be used.

  4. After decoding, the resulting decoded URL is returned.

  5. We create two variables: prevURL, which is empty, and decodeURL, which contains the URL to be decoded.

     Variable State:  
     prevURL = ""  
     decodeURL ="somethingvideo%252Fmp4"  
    

  6. We create an iteration that runs until prevURL!=decodeURL

  7. Now we update prevURL to decodeURL and update decodeURL with the decoded value of the URL passed.

     Variable State:  
     prevURL = "somethingvideo%252Fmp4"  
     decodeURL ="somethingvideo%2Fmp4"  
    

  8. As you can see, prevURL!=decodeURL, so we run it again.

     Variable State:  
     prevURL = "somethingvideo%2Fmp4"  
     decodeURL ="somethingvideo/mp4"  
    

  9. And again.

     Variable State:  
     prevURL = "somethingvideo/mp4"  
     decodeURL ="somethingvideo/mp4"  
    

  10. Now, prevURL=decodeURL, so the decoded URL is returned.

Output

 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  


Full Program

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


I hope this helps!

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:
java ,url encoding ,url decoding ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}