首页 > 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学习者快速成长!