Java 文件名上的 URL 编码/解码用 + 替换空格,需要替代。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22932060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL encode/decode on file name replace spaces with +, need alternative.
提问by Snow
My product is a web application.
I have files that I upload and download later on, to/from my server.
I am using java.net.URLDecoder.decode()when uploading files with unicode characters and java.net.URLDecoder.encode()when downloading files in order to save the file name and finally return it to the client as expected with no question marks and stuff (?????) .
The problem is that if the file name consists spaces then the encode/decode replace them with +character which is perfectly normal because that's their business implementation, but clearly as you can understand it does not fit to my purpose.
The question is what alternative do I have to overcome this situation?
Is there build-in method for that or 3rd party package?
我的产品是一个网络应用程序。
我有我稍后上传和下载到/从我的服务器的文件。
我使用java.net.URLDecoder.decode()时使用Unicode字符和上传文件java.net.URLDecoder.encode() ,以保存该文件名并最终返回给客户端下载文件时,如预期没有问题标记和东西(?????)。
问题是,如果文件名包含空格,则编码/解码将它们替换为+字符,这是完全正常的,因为这是他们的业务实现,但显然您可以理解它不符合我的目的。
问题是我有什么选择来克服这种情况?
该包或第 3 方包是否有内置方法?
采纳答案by Snow
I found the cure!
I was just needed to use java.net.URIfor that:
我找到了解药!
我只需要为此使用java.net.URI:
public static String encode(String urlString) throws UnsupportedEncodingException
{
try
{
URI uri = new URI(urlString);
return uri.toASCIIString();
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
}
The toASCIIString()escapes the special characters so when the string arrives to the browser it is shown correctly.
该toASCIIString()转义特殊字符,所以当串到达浏览器是正确显示。
回答by fge
You don't tell where this filename is used. The characters to encode will be different whether, for instance, it is in a URI query string or fragment part.
你不知道这个文件名在哪里使用。例如,无论是在 URI 查询字符串还是片段部分,要编码的字符都会有所不同。
You probably want to have a look at Guava's (15.0+) Escaper
s; and, in particular here, UnicodeEscaper
implementations and its derived class PercentEscaper
. Guava already provides a few of them usable in various parts of URLs.
你可能想看看 Guava's (15.0+) Escaper
s; 尤其是这里的UnicodeEscaper
实现及其派生类PercentEscaper
。Guava 已经提供了其中一些可用于 URL 的各个部分。
EDIT: here is how to do with Guava:
编辑:这是如何处理番石榴:
public final class FilenameEscaper
extends PercentEscaper
{
public PercentEscaper()
{
super("", false);
}
}
Done! See here. Of course, you may want to declare that some more characters than the default ones are safe.
完毕!见这里。当然,您可能希望声明比默认字符多一些的字符是安全的。
Also have a look at RFC 5987to make a better encoder.
另请查看RFC 5987以制作更好的编码器。
回答by Gregor Koukkoullis
You could also convert a space to %20.
您也可以将空格转换为 %20。
See: URL encoding the space character: + or %20?
There are also various other Java libraries that do URL encoding, with %20. Here are a two examples:
还有其他各种进行 URL 编码的 Java 库,带有 %20。这里有两个例子:
Guava:
番石榴:
UrlEscapers.urlPathSegmentEscaper().escape(urlToEscape);
Spring Framework:
弹簧框架:
UriUtils.encodePath(urlToEscape, Charsets.UTF_8.toString());
回答by Elvedin Hamzagic
This worked for me:
这对我有用:
URLEncoder.encode(someString, "UTF-8").replace("+", "%20");
回答by AlikElzin-kilaka
Had the same problem with spaces. Combination of URL and URI solved it:
空格也有同样的问题。URL 和 URI 的组合解决了它:
URL url = new URL("file:/E:/Program Files/IBM/SDP/runtimes/base");
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
* Please note that URLEncoderis used for web forms application/x-www-form-urlencoded
mime-type - not http network addresses.
* 请注意,URLEncoder用于 web 表单application/x-www-form-urlencoded
mime-type - 而不是 http 网络地址。