'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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomas Langer