Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8179507

URLDecoder wrongly replaces plus character with space

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_131"
      Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Version 6.1.7601

      A DESCRIPTION OF THE PROBLEM :
      The method URLDecoder#decode(String, String) method wrongly converts every plus-character to spaces. The plus character only represents a space in the query component of the URL, not the path component.

      This topic is discussed on StackOverflow:
      http://stackoverflow.com/questions/1005676/urls-and-plus-signs
      http://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      String input = "http://www.example.com/some+thing/";

      String output = URLDecoder.decode(input, "UTF-8");

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      the string remains unchanged:

      "http://www.example.com/some+thing/"
      ACTUAL -
      the plus is replaced with a space:

      "http://www.example.com/some thing/"

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.UnsupportedEncodingException;
      import java.net.URLDecoder;

      public class TestMain
      {

      public static void main(String[] args)
      {
      String input = "http://www.example.com/some+thing/";

      String output = null;
      try
      {
      output = URLDecoder.decode(input, "UTF-8");
      }
      catch(UnsupportedEncodingException e)
      {
      e.printStackTrace();
      }
      System.out.println(output);
      }

      }
      ---------- END SOURCE ----------

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: