This method encodes the URL, removes the spaces and brackets from the URL and replaces the same with "%20" and "%5B" and "%5D" and "%7B" "%7D". - Java Network

Java examples for Network:URL

Description

This method encodes the URL, removes the spaces and brackets from the URL and replaces the same with "%20" and "%5B" and "%5D" and "%7B" "%7D".

Demo Code

/**/*from  w  w  w  . ja v a  2 s  . c  o  m*/
 * Copyright (c) 2005-2006 Aptana, Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html. If redistributing this code,
 * this entire header must remain intact.
 */
//package com.java2s;

public class Main {
    /**
     * This method encodes the URL, removes the spaces and brackets from the URL and replaces the same with
     * <code>"%20"</code> and <code>"%5B" and "%5D"</code> and <code>"%7B" "%7D"</code>.
     * 
     * @param input
     * @return String
     * @since 3.0.2
     */
    public static String urlEncodeFilename(char[] input) {

        if (input == null) {
            return null;
        }

        StringBuffer retu = new StringBuffer(input.length);
        for (int i = 0; i < input.length; i++) {
            if (input[i] == ' ') {
                retu.append("%20"); //$NON-NLS-1$
            } else if (input[i] == '[') {
                retu.append("%5B"); //$NON-NLS-1$
            } else if (input[i] == ']') {
                retu.append("%5D"); //$NON-NLS-1$
            } else if (input[i] == '{') {
                retu.append("%7B"); //$NON-NLS-1$
            } else if (input[i] == '}') {
                retu.append("%7D"); //$NON-NLS-1$
            } else if (input[i] == '`') {
                retu.append("%60"); //$NON-NLS-1$
            } else if (input[i] == '+') {
                retu.append("%2B"); //$NON-NLS-1$
            } else {
                retu.append(input[i]);
            }
        }
        return retu.toString();
    }
}

Related Tutorials