Jackrabbit WebDAV同步示例?

时间:2014-09-15 19:30:30

标签: java apache webdav jackrabbit

我正在使用Jackrabbit库使用webdav协议与云存储进行通信。我需要一种方法来列出特定目录中的所有文件并获取最后修改的属性,但我似乎无法找到任何有用的示例。

我基本上需要代码来将本地目录中的文件与webdav网址同步。

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;

public class WebDavClient
{
    private String resourceUrl;
    private HttpClient client;
    private Credentials credentials;
    private DavMethod method;

    public WebDavClient(String resourceUrl, String username, String password)
            throws Exception
    {
        this.resourceUrl = resourceUrl;
        client = new HttpClient();
        credentials = new UsernamePasswordCredentials(username, password);
        client.getState().setCredentials(AuthScope.ANY, credentials);
    }

    public int upload(String fileToUpload) throws Exception
    {
        method = new PutMethod(getUpdatedWebDavPath(fileToUpload));

        RequestEntity requestEntity = new InputStreamRequestEntity(
                new FileInputStream(fileToUpload));
        ((PutMethod) method).setRequestEntity(requestEntity);

        client.executeMethod(method);

        return method.getStatusCode();
    }

    public int createFolder(String folder) throws Exception
    {
        method = new MkColMethod(getUpdatedWebDavPath(folder));

        client.executeMethod(method);

        return method.getStatusCode();
    }

    private String getUpdatedWebDavPath(String file)
    {
        // Make sure file names do not contain spaces
        return resourceUrl + "/" + new File(file).getName().replace(" ", "");
    }
}

将文件 Test.txt 上传到备份文件夹的使用示例:

String myAccountName = "...";
String myPassword = "...";
WebDavClient webdavUploader = new WebDavClient("https:\\\\webdav.hidrive.strato.com\\users\\" + myAccountName + "\\Backup", myAccountName, myPassword);

webdavUploader.upload("C:\\Users\\Username\\Desktop\\Test.txt");

以下是可能有用的不同DavMethods的列表: http://jackrabbit.apache.org/api/1.6/org/apache/jackrabbit/webdav/client/methods/package-summary.html

请帮忙,我一直在努力奋斗这么久!

1 个答案:

答案 0 :(得分:2)

在欧盟门户网站上查看来自Krusche和合作伙伴的AMES WebDAV客户端代码。它是根据GPL许可的,因此它可能适合您的目的。

https://joinup.ec.europa.eu/svn/ames-web-service/trunk/AMES-WebDAV/ames-webdav/src/de/kp/ames/webdav/WebDAVClient.java

它适用于我,但访问例如Win32LastModifiedTime我需要获取自定义命名空间,例如

private static final Namespace WIN32_NAMESPACE = Namespace.getNamespace("Z2", "urn:schemas-microsoft-com:");

并从属性中检索自定义属性Win32LastModifiedTime。

                /* 
                 * Win32LastModifiedTime
                 */
                String win32lastmodifiedtime = null;

                DavProperty<?> Win32LastModifiedTime = properties.get("Win32LastModifiedTime", WIN32_NAMESPACE);            
                if ((Win32LastModifiedTime != null) && (Win32LastModifiedTime.getValue() != null)) win32lastmodifiedtime = Win32LastModifiedTime.getValue().toString();