HttpClient = INVALID URI - Escaped absolute path not valid
Asked Answered
C

3

7

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".

this my 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

thanks

EDIT, FOUND the solution by replace space with "%20".

**

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

**

Confocal answered 30/11, 2012 at 20:28 Comment(1)
I had the same problem. But the URL contained a "-", in this case I needed to replace by %2D.Catling
C
5

I used this and get what I want...

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

Confocal answered 18/12, 2012 at 2:20 Comment(0)
H
2

use java.net.URLEncoder.encode

or replace your spaces with '+'

Honest answered 30/11, 2012 at 20:34 Comment(2)
+ is only acceptable in query strings and application/x-www-form-urlencoded POST data. Real encoding uses %20 here.Fricandeau
Thanks @Neil, I just update my question with the solution. %20 work great on the URL.Confocal
O
1

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).

Otology answered 30/11, 2012 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.