Java中的HTTP URL地址编码 [英] HTTP URL Address Encoding in Java
问题描述
我的 Java 独立应用程序从用户那里获得一个 URL(指向一个文件),我需要点击它并下载它.我面临的问题是我无法正确编码 HTTP URL 地址...
My Java standalone application gets a URL (which points to a file) from the user and I need to hit it and download it. The problem I am facing is that I am not able to encode the HTTP URL address properly...
示例:
URL: http://search.barnesandnoble.com/booksearch/first book.pdf
java.net.URLEncoder.encode(url.toString(), "ISO-8859-1");
还给我:
http%3A%2F%2Fsearch.barnesandnoble.com%2Fbooksearch%2Ffirst+book.pdf
但是,我想要的是
http://search.barnesandnoble.com/booksearch/first%20book.pdf
(空格被 %20 替换)
(space replaced by %20)
我猜 URLEncoder
不是为编码 HTTP URL 设计的...JavaDoc 说用于 HTML 表单编码的实用程序类"...还有其他方法可以做到这一点吗?
I guess URLEncoder
is not designed to encode HTTP URLs... The JavaDoc says "Utility class for HTML form encoding"... Is there any other way to do this?
推荐答案
java.net.URI 类可以提供帮助;在您找到的 URL 文档中
The java.net.URI class can help; in the documentation of URL you find
请注意,URI 类在某些情况下确实会对其组件字段进行转义.管理 URL 编码和解码的推荐方法是使用 URI
Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use an URI
使用具有多个参数的构造函数之一,例如:
Use one of the constructors with more than one argument, like:
URI uri = new URI(
"http",
"search.barnesandnoble.com",
"/booksearch/first book.pdf",
null);
URL url = uri.toURL();
//or String request = uri.toString();
(URI 的单参数构造函数不会转义非法字符)
只有非法字符会被上面的代码转义 - 它不会转义非 ASCII 字符(请参阅 fatih 的评论).toASCIIString
方法可用于获取仅包含 US-ASCII 字符的字符串:
Only illegal characters get escaped by above code - it does NOT escape non-ASCII characters (see fatih's comment).
The toASCIIString
method can be used to get a String only with US-ASCII characters:
URI uri = new URI(
"http",
"search.barnesandnoble.com",
"/booksearch/é",
null);
String request = uri.toASCIIString();
<小时>
对于查询如 http://www.google.com/ig/api?weather=São Paulo
的 URL,使用构造函数的 5 参数版本:
For an URL with a query like http://www.google.com/ig/api?weather=São Paulo
, use the 5-parameter version of the constructor:
URI uri = new URI(
"http",
"www.google.com",
"/ig/api",
"weather=São Paulo",
null);
String request = uri.toASCIIString();
这篇关于Java中的HTTP URL地址编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!