Java URI Encode encodeUri(String uri)

Here you can find the source of encodeUri(String uri)

Description

URL-encodes everything between "/"-characters.

License

Open Source License

Declaration

public static String encodeUri(String uri) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.StringTokenizer;

public class Main {
    /**//from w w w  .  j  a  va 2 s.c o m
     * URL-encodes everything between "/"-characters. Encodes spaces as '%20' instead of '+'.
     */
    public static String encodeUri(String uri) {
        String newUri = "";
        StringTokenizer st = new StringTokenizer(uri, "/ ", true);
        while (st.hasMoreTokens()) {
            String tok = st.nextToken();
            if (tok.equals("/")) {
                newUri += "/";
            } else if (tok.equals(" ")) {
                newUri += "%20";
            } else {
                try {
                    newUri += URLEncoder.encode(tok, "UTF-8");
                } catch (UnsupportedEncodingException ignored) {
                }
            }
        }
        return newUri;
    }
}

Related

  1. encodeUri(final String uri)
  2. encodeUri(final URI uri)
  3. encodeURI(String s)
  4. encodeUri(String string)
  5. encodeURI(String uri)
  6. encodeUri(String uri)
  7. encodeUri(URI uri)
  8. encodeURI(URI uri)
  9. encodeURIComponent(final String s)