首頁 > Java > java教程 > 主體

Java URL編碼器

PHPz
發布: 2024-08-30 16:10:02
原創
831 人瀏覽過

Java URLEncoder 是一個支援 HTML 表單編碼的實用程式類別。使用 Java 的 URLEncoder 類別實用程序,HTML 的形式變得更加可靠和穩定。每當使用者呼叫 get 方法時,編碼器都會在 URL 末尾附加特殊字元、值和參數,這使得 URL 在某種程度上未經身份驗證。此外,該值還使用特殊字符,進一步僅使用 HTML 來執行所有操作的順利處理。當 Java URLEncoder 及其定義的實用程式類別存在時,完全依賴 HTML 根本不是一個好習慣。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

public static String encode(String st, String enc1)throws UnsupportedEncodingException
登入後複製

語法流程的參數如下:

  • String st: 此參數提供字串作為需要傳遞給函數 String 編碼的輸入。
  • 字串 enc1: 此參數提供字串 enc,它用作一種方法,或者說,一個編碼過程,與字串 c 一起用作其工作的參數。
  • Throws: throws 關鍵字的行為類似。它是一個用於捕獲所有可能妨礙Java URLEncoder 實用程式類別的異常的函數。如果不使用指定的編碼,則強制需要。

Java URLEncoder 類別如何運作?

URLEncoder 是用於任何 HTML 編碼的 Java 類別的實用程式。當 URLEncoder 的 Java 實用程式類別的存在可以順利地增強字串轉換的活動類型時,每次使用 HTML 編碼,即使對於小型和簡化的方法調用,也是一種不需要的活動。

當涉及字串及其從字元解析和編碼、解碼特殊字串因素的轉換時,它被認為是最安全可靠的實用程式類別之一。此外,它還利用內建功能,該功能廣泛用於將字串轉換為必要的格式,然後在使用URLEncoder 對字串進行編碼時應用於該字串的一些行為準則或規則,如下所示:

  • 實用程式類別中使用的所有字母數字字元和某些特殊字元(例如“*”、“_”、“-”和“.”)保持不變且不會通知。
  • 所有多餘的空格都需要轉換為「+」號。
  • 字串中的所有剩餘字元或其他字元都是透過單獨解析編碼字串來編碼的,或者它可以是多個位元組,用於根據指定的方案對任何字串進行編碼。然後,這些字串位元組進一步轉換為具有 %xy 形式的某種字串格式的三字元字串,其中 xy 表示十六進位格式的字元編碼字串。
  • UTF 格式主要是 W3C 認可的建議標準,用於與字串相關的任何類型的編碼策略。

一個範例將闡明字串編碼需要遵循 UTF 格式標準,這表示如果我們有一些參數或值包含一些特殊字元和空格的值,則透過範例進行示範:

  • u@educba 學習:如果編碼值或字元是@,那麼它是按UTF-8使用的,這是接受編碼字串類型的最常規方式,@符號將被轉換為40%,其餘帶有空格的值將轉換為+ 符號,這將產生一個字串作為輸出,它看起來有點像:
  • u%40educba+for+learning:該類別作為實用程式提供的方法僅包括一種用於實現該目的的方法,該方法被定義為encode()。
  • encode() 是支援此 Java Utility 類別的唯一方法。顧名思義,它是用於編碼目的的方法,然後傳回任何指定字串的編碼字串。此外,它不提供透過其操作順利地服務於編碼過程的靈活性。因此,它創建了下一個版本中發布的另一個方法,它甚至可以捕獲所有異常,然後該編碼功能是實用程式類別所需的輸出。

Java URLEncoder 範例

以下是java urlencoder的範例:

Example #1

This program is used to illustrate the URLEncoder utility of Java where the input string is given as the base url for the link and then a string query for retrieving the final string using UTF-8 as a conventional standard for encoding. Output is shown where one encoded string is without URL and the other with UTF-8 standard, which comprises the URL.

Code:

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class UrlEncoderJava
{
public static void main(String[] args) throws MalformedURLException,
UnsupportedEncodingException
{
String baseurl = "https://www.educba.com/?q=";
String query = "u@educba for educba";
System.out.println("Without encoding URL :");
URL url = new URL(baseurl + query);
System.out.println(url);
System.out.println("URL after encoding :");
url = new URL(baseurl + URLEncoder.encode(query, "UTF-8"));
System.out.println(url);
}
}
登入後複製

Output:

Java URL編碼器

Example #2

This program is used to represent the encoded string which makes use of the standard Charsets of the UTF_8 to the string and then provides the entire encoded string as shown in the output after converting the URL link with the defined standard and Encoder class of java.

Code:

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.io.UnsupportedEncodingException;
public class URLEncodingParsing {
private static String encodingOfValue(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex.getCause());
}
}
public static void main(String[] args) {
String baseUrl = "https://www.educba.com/search?q=";
String query = "educba@Java@lang";
String encodedQuery = encodingOfValue(query);
String completeUrl = baseUrl + encodedQuery;
System.out.println(completeUrl);
}
}
登入後複製

Output:

Java URL編碼器

Conclusion

URLEncoder in java is a utility class that provides aid for the HTML related forms to encode the special characters being provided for parsing. The UTF-8 standard recommended by W#C has enhanced the overall encoding method for encoding and conversion of the encoded string to the final string after parsing. Using this class for string encoding is a very reliable and secured form of coding; as always, making HTML is not preferred.

以上是Java URL編碼器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!