0

I have a problem with the encoding of image URLs on my website and I tried 2 different encoding methods but they both have problems. For example my image file has the following name: "Some special Characters;.jpg"

If I use URLEncoder.encode(imageName, "UTF-8"); I get the following URL:

/some/url/Some+special+Characters%3B_92631.jpg

If I use UrlEscapers.urlPathSegmentEscaper().escape(imageName) I get:

/some/url/Some%20special%20Characters;_63414.jpg

I tried to find the correct URL of the image and I found this one:

/some/url/Some%20special%20Characters%3B_63414.jpg

It looks like URLEncoder is not encoding spaces correctly while URLEscapers does not encode the semicolon. Other special characters like ! or , work without any problems.

How can I encode my URL correctly?

Fynn
  • 3
  • 1
  • 1
    `URLEncoder`, despite its name, is for encoding URL `arguments`, not URLs themselves. You need to look at `URI(String, String, String)` for this. – user207421 Oct 04 '21 at 09:02
  • I founded an question look like what you asking: https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters let vote for me if it's works =)) – Thao Nguyen Oct 04 '21 at 09:03
  • @user207421 Interesting fact. My problem is URI seems not to allow spaces in the string so URI(imageName) does not work as it throws an Exception. Any idea? – Fynn Oct 04 '21 at 11:55
  • @ThaoNguyen as I am not trying to encode parameters but the URL itself as user207421 mentioned so I don't think this solution will work for me. But thanks :) – Fynn Oct 04 '21 at 11:57

0 Answers0