Subbu
Subbu

Reputation: 83

Creating a new directory from the application

Using tomcat webdav, currently I am able to upload files/documents into a particular webdav location. To this extent, it is fine.

However now I have a requirement that I have to create a new directory (based on a condition) in the webdav location and upload the files or documents into that newly created directory.

This I have to achieve through Java. I tried to create a directory using file.mkdir(). But it is returning false to me.

Can anyone throw some light how can I overcome this situation?

Kind regards

Subbu

Upvotes: 1

Views: 3172

Answers (2)

Subbu
Subbu

Reputation: 83

Thank you Christopher!!

I would like to illustrate my code to create a webdav folder using a sample application.

public class CreateWebdavFolderUsingHttpClient {

    private static String url = "http://localhost:8081/webdav/subbu/MyWebdavFolder";

    public static void main(String[] args) {
        String response = null;
        /* Create an instance of HttpClient.*/
        HttpClient client = new HttpClient();
        client.getState().setCredentials(
                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials("tomcat", "tomcat"));

        DavMethod method = new MkColMethod(url);
        try {
            client.executeMethod(method);
            response = method.getStatusCode() + " - " + method.getStatusText();
            client.executeMethod(new MkColMethod(url + "/10300"));
            response = method.getStatusCode() + " - " + method.getStatusText();
        } catch (IOException ex) {
            // Catch error
        } finally {
            method.releaseConnection();
        }
        System.out.println(response);
    }
}

Upvotes: 0

Christopher Schultz
Christopher Schultz

Reputation: 20862

Make an HTTP connection to your WebDAV service and issue a MKCOL request (MKCOL is just like GET, POST, etc.). Something like this:

MKCOL /the/directory/you/want/to/create HTTP/1.1

You should expect a 201 response. If you get something else, it means that the directory creation has failed.

Upvotes: 1

Related Questions