urldecode

如何在Java中进行UrlDecode

小樊
96
2024-06-18 00:18:53
栏目: 编程语言

在Java中进行UrlDecode可以使用java.net包中的URLDecoder类。下面是一个简单的示例代码来展示如何使用URLDecoder进行UrlDecode操作:

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class UrlDecodeExample {
    public static void main(String[] args) {
        String encodedUrl = "https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Djava%2Burldecode";
        
        try {
            String decodedUrl = URLDecoder.decode(encodedUrl, "UTF-8");
            System.out.println("Decoded URL: " + decodedUrl);
        } catch (UnsupportedEncodingException e) {
            System.out.println("Unsupported Encoding Exception: " + e.getMessage());
        }
    }
}

在上面的示例中,我们传入待解码的URL和编码格式给URLDecoder的decode方法,然后打印出解码后的URL。如果URL中包含非法的编码字符,会抛出UnsupportedEncodingException异常。

0
看了该问题的人还看了