title: Why does Java automatically decode %2F in URI encoded filenames?
body: <p>I have a servlet that needs to write out files that have a user-configurable name.  I am trying to use URI encoding to properly escape special characters, but the JRE appears to automatically convert encoded forward slashes <code>%2F</code> into path separators.</p>
  
  <p>Example:</p>
  
  <pre><code>File   dir = new File("C:\Documents and Setting\username\temp");
  String fn  = "Top 1/2.pdf";
  URI    uri = new URI( dir.toURI().toASCIIString() + URLEncoder.encoder( fn, "ASCII" ).toString() );
  File   out = new File( uri );
  
  System.out.println( dir.toURI().toASCIIString() );
  System.out.println( URLEncoder.encode( fn, "ASCII" ).toString() );
  System.out.println( uri.toASCIIString() );
  System.out.println( output.toURI().toASCIIString() );
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>file:/C:/Documents%20and%20Settings/username/temp/
  Top+1%2F2.pdf   
  file:/C:/Documents%20and%20Settings/username/temp/Top+1%2F2.pdf
  file:/C:/Documents%20and%20Settings/username/temp/Top+1/2.pdf
  </code></pre>
  
  <p>After the new File object is instantiated, the <code>%2F</code> sequence is automatically converted to a forward slash and I end up with an incorrect path.  Does anybody know the proper way to approach this issue?  </p>
  
  <p>The core of the problem seems to be that</p>
  
  <pre><code>uri.equals( new File(uri).toURI() ) == FALSE
  </code></pre>
  
  <p>when there is a <code>%2F</code> in the URI.</p>
  
  <p>I'm planning to just use the URLEncoded string verbatim rather than trying to use the <code>File(uri)</code> constructor.</p>