URLEncoder.encode()和空格? [英] URLEncoder.encode() and a whitespace?

查看:854
本文介绍了URLEncoder.encode()和空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上有一个资源:

I've got a resource on my server named:

some image.png

名称中有一个空格.当我在浏览器(chrome)中输入url时,它会将空格转换为%20:

There's a space in the name. When I type the url into the browser (chrome), it's transforming the space into %20:

some%20.png

当我从应用程序中使用URLEncoder.encode("some image.png")时,我得到了:

WHen I use URLEncoder.encode("some image.png") from my application, I'm getting:

some+image.png

这会导致404.正确的编码方式是什么?

which causes a 404. What's the right way to encode?

推荐答案

http://www.permadi .com/tutorial/urlEncoding/

请注意,由于该字符非常常用,因此保留了特殊代码("+"号)作为其URL编码.因此,字符串"A B"可以URL编码为"A%20B"或"A + B"

Note that because the character is very commonly used, a special code ( the "+" sign) has been reserved as its URL encoding. Thus the string "A B" can be URL encoded as either "A%20B" or "A+B"

但是,您的代码可能会失败,因为某些解析器/Web服务器无法对基本URL部分进行相同的处理-只能在查询字符串部分识别+ = space

However, your code is probably failing because some parsers/web servers do not handle them equally for base URL portions - and only recognize + = space in the querystring portion

这篇关于URLEncoder.encode()和空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆