Java UTF8 Encode encodeUtf8(String url)

Here you can find the source of encodeUtf8(String url)

Description

encode Utf

License

Apache License

Declaration

public static String encodeUtf8(String url) 

Method Source Code


//package com.java2s;
/*/*  w  w w .  j a  va2  s .co m*/
 * Copyright (c) 2008-2017 Haulmont.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Main {
    public static String encodeUtf8(String url) {
        try {
            return URLEncoder.encode(url, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unable to encode using charset");
        }
    }

    public static String encode(String url, Charset charset) {
        try {
            return URLEncoder.encode(url, charset.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unable to encode using charset");
        }
    }
}

Related

  1. encodeUTF8(String str)
  2. encodeUtf8(String string)
  3. encodeUtf8(String target)
  4. encodeUTF8(String text)
  5. encodeUTF8(String text)
  6. getBufferedWriter(final File outFile, final String outEncoding)
  7. getUTF8Bytes(String string)
  8. getUTF8Bytes(String string)
  9. getUTF8Decoder(boolean ignoreEncodingErrors)