java HttpClient = INVALID URI - 转义的绝对路径无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13652681/
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
HttpClient = INVALID URI - Escaped absolute path not valid
提问by david
I am trying to upload/delete a file to webdav server using HttpClient. However, none is working whenever I have a file name consist of space . I got a error message saying "INVALID URI--- Escaped absolute path not valid".
我正在尝试使用 HttpClient 将文件上传/删除到 webdav 服务器。但是,每当我的文件名由 space 组成时,都没有工作。我收到一条错误消息,指出“无效的 URI --- 转义的绝对路径无效”。
this my URL = "http://localhost:8080/test file.txt"
这是我的 URL = "http://localhost:8080/test file.txt"
private boolean delete(String fileName) {
HttpClient client = new HttpClient();
HttpHost host = new HttpHost(WEBDAV_URL, PORT_NUMBER);
client.getHostConfiguration().setHost(host);
DeleteMethod del = new DeleteMethod(WEBDAV_URL_COMPLETE + fileName);
try {
client.executeMethod(del);
return true;
} catch (HttpException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
is there any method or URL parse should I use to fix the problem
我应该使用任何方法或 URL 解析来解决问题吗
thanks
谢谢
EDIT, FOUND the solution by replace space with "%20".
编辑,通过用“ %20”替换空格找到了解决方案。
**
**
URL.replaceAll(" ","%20")
URL.replaceAll(" ","%20")
**
**
采纳答案by david
I used this and get what I want...
我用了这个,得到了我想要的......
URL.replaceAll(" ","%20")
URL.replaceAll(" ","%20")
回答by Stéphane
use java.net.URLEncoder.encode
利用 java.net.URLEncoder.encode
or replace your spaces with '+'
或用“+”替换您的空格
回答by ninnemannk
You should simply rename your file as:
您应该简单地将文件重命名为:
test_file.txt or textFile.txt
It is common standard to never use spaces when coding variables or creating files for such.
在编码变量或为其创建文件时从不使用空格是常见的标准。
Use test_file (snake case) or textFile (camelCase).
使用 test_file (snake case) 或 textFile (camelCase)。