Decode URL or input using latin characters (for HTTP GET and database). - Java java.net

Java examples for java.net:URL Encode

Introduction

The following code shows how to Decode URL or input using latin characters (for HTTP GET and database).

Demo Code

//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class Main {
    public static void main(String[] argv) {
        String input = "java2s.com";
        System.out.println(decodeLatin1(input));
    }/*from ww w.  ja  v  a 2  s.c  o  m*/

    /**
     * <p>Decode URL or input using latin characters (for HTTP GET and database).</p>
     * @param input encoded URL or text
     * @return ASCII string.
     */
    public static String decodeLatin1(final String input) {
        String result = null;

        try {
            if (input != null) {
                result = URLDecoder.decode(input, "Latin1");
            }
        } catch (UnsupportedEncodingException e) {
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * <p>Decode URL or input using latin characters (for HTTP GET and database).</p>
     * @param input encoded URL or text
     * @return ASCII string.
     */
    public static String decodeLatin1(final String input,
            final String delimiter) {
        String result = null;

        if (input != null) {
            try {

                result = URLDecoder.decode(input, "Latin1");
            } catch (Exception e) {
            }

            if (result == null) {
                int indexLastSeparator = input.lastIndexOf(delimiter);
                if (indexLastSeparator != -1) {
                    String truncatedInput = input.substring(0,
                            indexLastSeparator);
                    try {
                        result = URLDecoder
                                .decode(truncatedInput, "Latin1");
                    } catch (Exception e) {
                    }
                }
            }
        }

        return result;
    }
}

Related Tutorials