views:

1039

answers:

3

I have a string representing an URL containing spaces and want to convert it to an URI object. If is simple try to do

String myString = "http://myhost.com/media/mp3s/9/Agenda of swine - 13. Persecution Ascension_ leave nothing standing.mp3";
URI myUri = new URI(myString);

it gives me

java.net.URISyntaxException: Illegal character in path at index X

where index X is the position of the first space in the URL string.

How can i parse myStringinto a URI object?

+2  A: 
java.net.URLEncoder.encode(finalPartOfString, "utf-8");

This will URL-encode the string.

finalPartOfString is the part after the last slash - in your case, the name of the song, as it seems.

Bozho
It will also urlencode the colon and the slashes which would make the url still invalid. He basically only need to urlencode the spaces to get it valid.
BalusC
@BalusC, thanks, I added an update.
Bozho
Ok, this gets me by the `URISyntaxException` but now i get a 404 from the server. The url I get is `http://myhost.com/media/mp3s/9/Agenda+of+swine+-+13.+Persecution+Ascension_+leave+nothing+standing.mp3`. I use the URI in an `org.apache.http.client.methods.HttpGet.HttpGet` Request. Any ideas?
Mannaz
@Mannaz now that's another thing - you have to show the servlet code - or better, ask another question. The problem is no longer on the client.
Bozho
@Bozho shure it is a client/encoding problem, because requesting the original URL (`myString`) in a normal Browser does not result in a 404 error.
Mannaz
@Mannaz and does the resultant (encoded) string result in 404 in a browser?
Bozho
A: 
Dave.B
Two flaws: 1) replaceAll is unnecessary. 2) the spaces are not to be represented as underscores.
BalusC
this is not reversable. On the server side you can't be sure whether it wasn't an underscore originally.
Bozho
+2  A: 

You should in fact URLEncode the "invalid" characters. Since the string actually contains the complete URL, it's hard to properly URL-encode it. You don't know which slashes / should be taken into account and which not. You cannot predict that on a raw String beforehand. The problem really needs to be solved at a higher level. Where does that String come from? Is it hardcoded? Then just change it yourself accordingly. Does it come in as user input? Validate it and show error, let the user solve itself.

At any way, if you can ensure that it are only the spaces in URL's which makes it invalid, then you can also just do a char-by-char replace with +:

URI uri = new URI(string.replace(' ', '+'));

or a string-by-string replace with %20:

URI uri = new URI(string.replace(" ", "%20"));

Or if you can ensure that it's only the part after the last slash which needs to be encoded, then you can also just do:

int pos = string.lastIndexOf('/') + 1;
URI uri = new URI(string.substring(0, pos) + URLEncoder.encode(string.substring(pos), "UTF-8"));
BalusC
Replacing the spaces with `%20` dit the trick. THX
Mannaz
@Mannaz - just be careful when another "invalid" symbol appears in a song name.
Bozho