Java中的HTTP URL地址编码

本文翻译自:HTTP URL Address Encoding in Java

My Java standalone application gets a URL (which points to a file) from the user and I need to hit it and download it. 我的Java独立应用程序从用户获取一个URL(指向一个文件),我需要点击它并下载它。 The problem I am facing is that I am not able to encode the HTTP URL address properly... 我面临的问题是我无法正确编码HTTP URL地址......

Example: 例:

URL:  http://search.barnesandnoble.com/booksearch/first book.pdf

java.net.URLEncoder.encode(url.toString(), "ISO-8859-1");

returns me: 回报我:

http%3A%2F%2Fsearch.barnesandnoble.com%2Fbooksearch%2Ffirst+book.pdf

But, what I want is 但是,我想要的是

http://search.barnesandnoble.com/booksearch/first%20book.pdf

(space replaced by %20) (空间由%20取代)

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? 我想URLEncoder不是为编码HTTP URL而设计的... JavaDoc说“用于HTML表单编码的实用程序类”......有没有其他方法可以做到这一点?


#1楼

参考:https://stackoom.com/question/32M7/Java中的HTTP-URL地址编码


#2楼

也许可以在org.springframework.web.util中尝试UriUtils

UriUtils.encodeUri(input, "UTF-8")

#3楼

String url="" http://search.barnesandnoble.com/booksearch/ ; String url =“” http://search.barnesandnoble.com/booksearch/ ;

This will be constant i guess and only filename changes dyamically so get filename 这将是不变的我猜,只有文件名动态地改变所以得到文件名

String filename; String filename; // get the file name //获取文件名

String urlEnc=url+fileName.replace(" ","%20"); String urlEnc = url + fileName.replace(“”,“%20”);


#4楼

I develop a library that serves this purpose: galimatias . 我开发了一个服务于此目的的库: galimatias It parses URL the same way web browsers do. 它以与Web浏览器相同的方式解析URL。 That is, if a URL works in a browser, it will be correctly parsed by galimatias . 也就是说,如果URL在浏览器中工作,它将被galimatias正确解析。

In this case: 在这种情况下:

// Parse
io.mola.galimatias.URL.parse(
    "http://search.barnesandnoble.com/booksearch/first book.pdf"
).toString()

Will give you: http://search.barnesandnoble.com/booksearch/first%20book.pdf . 会给你: http://search.barnesandnoble.com/booksearch/first%20book.pdfhttp://search.barnesandnoble.com/booksearch/first%20book.pdf Of course this is the simplest case, but it'll work with anything, way beyond java.net.URI . 当然这是最简单的情况,但除了java.net.URI之外,它还可以处理任何事情。

You can check it out at: https://github.com/smola/galimatias 您可以在以下网址查看: https//github.com/smola/galimatias


#5楼

You can use a function like this. 你可以使用这样的功能。 Complete and modify it to your need : 完成并根据您的需要进行修改:

/**
     * Encode URL (except :, /, ?, &, =, ... characters)
     * @param url to encode
     * @param encodingCharset url encoding charset
     * @return encoded URL
     * @throws UnsupportedEncodingException
     */
    public static String encodeUrl (String url, String encodingCharset) throws UnsupportedEncodingException{
            return new URLCodec().encode(url, encodingCharset).replace("%3A", ":").replace("%2F", "/").replace("%3F", "?").replace("%3D", "=").replace("%26", "&");
    }

Example of use : 使用示例:

String urlToEncode = ""http://www.growup.com/folder/intérieur-à_vendre?o=4";
Utils.encodeUrl (urlToEncode , "UTF-8")

The result is : http://www.growup.com/folder/int%C3%A9rieur-%C3%A0_vendre?o=4 结果是: http//www.growup.com/folder/int%C3%A9rieur-%C3%A0_vendre?o = 4


#6楼

Please be warned that most of the answers above are INCORRECT. 请注意,上面的大多数答案都是不正确的。

The URLEncoder class, despite is name, is NOT what needs to be here. URLEncoder类,尽管是名称,但不是必须在这里。 It's unfortunate that Sun named this class so annoyingly. 令人遗憾的是,Sun这个课程如此令人讨厌。 URLEncoder is meant for passing data as parameters, not for encoding the URL itself. URLEncoder用于将数据作为参数传递,而不是用于对URL本身进行编码。

In other words, "http://search.barnesandnoble.com/booksearch/first book.pdf" is the URL. 换句话说, "http://search.barnesandnoble.com/booksearch/first book.pdf"是URL。 Parameters would be, for example, "http://search.barnesandnoble.com/booksearch/first book.pdf?parameter1=this&param2=that" . 参数例如是"http://search.barnesandnoble.com/booksearch/first book.pdf?parameter1=this&param2=that" The parameters are what you would use URLEncoder for. 这些参数是您使用URLEncoder的参数。

The following two examples highlights the differences between the two. 以下两个例子突出了两者之间的差异。

The following produces the wrong parameters, according to the HTTP standard. 根据HTTP标准,以下内容产生错误的参数。 Note the ampersand (&) and plus (+) are encoded incorrectly. 请注意,&符号(&)和加号(+)编码不正确。

uri = new URI("http", null, "www.google.com", 80, 
"/help/me/book name+me/", "MY CRZY QUERY! +&+ :)", null);

// URI: http://www.google.com:80/help/me/book%20name+me/?MY%20CRZY%20QUERY!%20+&+%20:)

The following will produce the correct parameters, with the query properly encoded. 以下将生成正确的参数,并对查询进行正确编码。 Note the spaces, ampersands, and plus marks. 请注意空格,&符号和加号。

uri = new URI("http", null, "www.google.com", 80, "/help/me/book name+me/", URLEncoder.encode("MY CRZY QUERY! +&+ :)", "UTF-8"), null);

// URI: http://www.google.com:80/help/me/book%20name+me/?MY+CRZY+QUERY%2521+%252B%2526%252B+%253A%2529
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值