Alex Broadwin
Alex Broadwin

Reputation: 1338

Java URI doesn't encode semicolons in path. Best practice here?

I'm working on a project where a local file is exported via HTTP. This involves getting a file URI, relativizing it using the exported path, tacking it onto the export URI and then handling that as a URL on the receiving end.

Normally this works fine, but I run into trouble when the filename contains a semicolon. I narrowed it down to here:

new File(path).toURI()

The above method correctly encodes spaces and the like, but not semicolons (which should be encoded into a %3B).

Ultimately the above method returns the result of the URI constructor (protocol, host, path, fragment), which returns the bad URI.

I could manually replace all semicolons with %3B, but that doesn't feel like the best solution. Is there really no built-in API to correctly encode a path?

Many thanks for any assistance.

Upvotes: 2

Views: 1983

Answers (2)

Tomas Langer
Tomas Langer

Reputation: 499

The reason semicolon is not escaped automatically is because it has a meaning in the URI specification - it delimits "path parameters". The following URI si valid: /some;a=b/path

and represents path /some/path with a path parameter a of value b.

So in this case the escape must be manual, because URI cannot determine whether the semicolon delimits parameters or is part of the path.

Upvotes: 0

ZhongYu
ZhongYu

Reputation: 19682

Semicolon is a perfectly valid char in URIs. Of course if the receiving end uses semicolon as a special delimiter, the sender needs to escape it. But that's outside the standard practice, so you'll have to escape it yourself.

But in the java world, servlet is the standard, and it uses semicolon as special delimiters. I'm not aware of any utility to help you there, so you'll still need to manually escape semicolons.

Upvotes: 2

Related Questions