How to encoding and decoding URL in Java

2 Answers

0 votes
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Base64.Encoder;

public class MyClass {
    public static void main(String args[]) throws UnsupportedEncodingException {
        String url =  "https://www.collectivesolver.com";

        byte[] bytes_array = url.getBytes("UTF-8");
        
        Encoder encoder = Base64.getUrlEncoder();
        String encode_string = encoder.encodeToString(bytes_array);
        System.out.println(encode_string);

        String decode_string = new String(Base64.getUrlDecoder().decode(encode_string));
        System.out.println(decode_string);
    }
}




/*
run:

aHR0cHM6Ly93d3cuY29sbGVjdGl2ZXNvbHZlci5jb20=
https://www.collectivesolver.com

*/

 



answered Jan 3, 2022 by avibootz
edited Oct 21, 2023 by avibootz
0 votes
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.io.UnsupportedEncodingException;

public class MyClass {
    public static void main(String args[]) throws UnsupportedEncodingException {
        String url =  "https://www.shortinfos.com";

        String encodeUrl = URLEncoder.encode(url, StandardCharsets.UTF_8.toString());
        System.out.println(encodeUrl);
 
        String decodedUrl = URLDecoder.decode(encodeUrl, "UTF-8");
        System.out.println(decodedUrl);
    }
}
 
 
 
 
/*
run:
 
https%3A%2F%2Fwww.shortinfos.com
https://www.shortinfos.com
 
*/

 



answered Oct 21, 2023 by avibootz

Related questions

1 answer 174 views
1 answer 195 views
1 answer 201 views
1 answer 145 views
145 views asked Jan 27, 2024 by avibootz
1 answer 121 views
1 answer 144 views
...