0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

java。URLエンコード。標準ライブラリ。spring frameworkのライブラリの例

Posted at

Java標準ライブラリを使用したURLエンコード

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

public class URLEncodeExample {
    public static void main(String[] args) {
        try {
            String original = "こんにちは 世界!";
            String encoded = URLEncoder.encode(original, "UTF-8");
            System.out.println("Original: " + original);
            System.out.println("Encoded: " + encoded);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

出力

Original: こんにちは 世界!
Encoded: %E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF+%E4%B8%96%E7%95%8C%21

Spring FrameworkのUriComponentsBuilderを使用したURLエンコード

import org.springframework.web.util.UriComponentsBuilder;

public class SpringURLEncodeExample {
    public static void main(String[] args) {
        String baseUrl = "https://example.com/search";
        String queryParam = "こんにちは 世界!";

        String url = UriComponentsBuilder.fromHttpUrl(baseUrl)
                .queryParam("q", queryParam)
                .encode()
                .toUriString();

        System.out.println("Encoded URL: " + url);
    }
}

出力

Encoded URL: https://example.com/search?q=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF+%E4%B8%96%E7%95%8C%21
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?