Skip to content

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.

Answer

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.