1

I have some Java code that takes a String and converts a space to a +. But when my programs reads that String back, it converts the + to a %20.

Is there any way for me to keep it consistent so that it:

  1. Either converts the space straight to %20, OR
  2. Doesnt convert the+to%20`

Here`s what my code looks like:

import java.net.URLEncoder;
...
String fileNameEncoded = URLEncoder.encode(filename);

So if filename was foo bar.html, first pass would give me foo+bar.html and second pass would give me foo%20bar.html.

FilmiHero
  • 2,306
  • 7
  • 31
  • 46
  • 2
    What second pass? What programs? What java code? Try to look at your own question from the standpoint of someone who is willing to help you but has no knowledge of your environment, your purpose, you trace output, etc. "Garbage in, garbage out" applies in spades to asking questions about technical matters. If possible, and it looks like it might be in this case, write a small program that illustrates what you are talking about, since a small running program is usually unambiguous, at least. – arcy Mar 11 '13 at 20:28
  • 1
    Why are you url-encoding a string twice. One pass is sufficient. If you want to do it twice, then decode it twice as well. – JB Nizet Mar 11 '13 at 20:29
  • Sorry but I don't understand what other information you'd need. If you simply run the code I provided above w/ the example input of `foo bar.html` as the `filename`, you'll understand what I'm talking about. – FilmiHero Mar 11 '13 at 20:32
  • @JBNizet I think that was the key. I was encoding it twice instead of encoding once and decoding a second time. – FilmiHero Mar 11 '13 at 20:35
  • I have run the above program. `fileNameEncoded` is `"foo+bar.html"` as promised. If I extrapolate, and run `URLDecoder.decode`, I get back `"foo bar.html"`. So it seems that the problem lies in the code you're refusing to show us. – Nathaniel Waisbrot Mar 11 '13 at 20:37
  • If you had provided a small test case (and 3 lines of code would have been sufficient here), the answer would have been obvious and immediate. – JB Nizet Mar 11 '13 at 20:37
  • Absolutely unbelievable! I clearly provided the code above and explained, twice, that I am passing in String `foo bar.html` the first which comes out as `foo+bar.html`. Then, I'm passing it in again which comes out `foo%20bar.html`. I don't have any other code to show you all. Its as simple as that. – FilmiHero Mar 11 '13 at 21:23
  • I ran into this same issue. The " " space characters being replaced with the "+" plus-sign character is not acceptable. When I feed a URL having plus-signs in-place of spaces into my JAVA code, returns a 505 on the last line listed. ------ URL url = new URL(URLEncoder.encode(sourceFilePath)); URLConnection urlc = url.openConnection(); sourceInfo.stream = urlc.getInputStream(); ------ I believe that in my case it's the Tomcat version that we're connecting to, but regardless, the issue is that spaces are not acceptable... the URLEncoder.encode method should replace spaces with "%20", not "+". – MattWeiler Oct 31 '14 at 14:56
  • http://stackoverflow.com/questions/4737841/urlencoder-not-able-to-translate-space-character But I don't agree, the "+" may be the standard, but it isn't actually used across the board. – MattWeiler Oct 31 '14 at 14:57

1 Answers1

2

For understanding how "space straight to %20" we should at first understand why we are encoding a URL, the answer is because there are characters in URL which are not allowed for URL format like "&", "="," " and etc. as they will cause miss parsing of URL. For that reason URLEncoder replaces these characters to some conventional other characters which are allowed in URL format. and as URL parser seas those conventional characters it replaces back to "&", "="," " and etc.

Grigor Nazaryan
  • 567
  • 5
  • 18