How to do URL decoding in Java?

前端 未结 9 1566
轮回少年
轮回少年 2020-11-22 05:01

In Java, I want to convert this:

https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type

To thi

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 05:23

    Using java.net.URI class:

    public String getDecodedURL(String encodedUrl) {
        try {
            URI uri = new URI(encodedUrl);
            return uri.getScheme() + ":" + uri.getSchemeSpecificPart();
        } catch (Exception e) {
            return "";
        }
    }
    

    Please note that exception handling can be better, but it's not much relevant for this example.

提交回复
热议问题