7

我正在尝试使用 HttpClient 将文件上传/删除到 webdav 服务器。但是,只要我的文件名包含空格,就没有任何工作。我收到一条错误消息,提示“无效的 URI --- 转义的绝对路径无效”。

这是我的 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;
    }
}

我应该使用任何方法或 URL 解析来解决问题吗

谢谢

编辑,通过用“ %20 ”替换空间来找到解决方案。

**

URL.replaceAll(" ","%20")

**

4

3 回答 3

5

我用这个得到了我想要的......

URL.replaceAll(" ","%20")

于 2012-12-18T02:20:47.873 回答
2

采用java.net.URLEncoder.encode

或用“+”替换你的空格

于 2012-11-30T20:34:24.730 回答
1

您只需将文件重命名为:

test_file.txt or textFile.txt

在编码变量或为其创建文件时,从不使用空格是常见的标准。

使用 test_file (snake case) 或 textFile (camelCase)。

于 2012-11-30T20:46:32.540 回答