如何通过Apache Jackrabbit库从WebDAV获取文件内容?

时间:2015-12-03 10:02:42

标签: java webdav jackrabbit

有一个很好的示例显示如何将文件放到WebDAV服务器上:

  

Java: How to upload a file to a WebDAV server from a servlet?

但我怎样才能获得文件内容?

PUT命令有MethodPut个类,但没有合适的GetMethod(尽管提供了枚举DavMethods.METHOD_GET)。

1 个答案:

答案 0 :(得分:0)

我解决了任务1)为GET方法实现我自己的类2)读取代表文件内容的响应字节。我宁愿在Jackrabbit找到一个更简单的解决方案。

public class MyGetMethod extends DavMethodBase {
    public MyGetMethod(String uri) {
        super(uri);
    }
    public String getName() {
        return DavMethods.METHOD_GET;
    }
    public boolean isSuccess(int statusCode) {
        return statusCode == 200;
    }
}

static void jackrabbitGet() throws Exception {
    HttpClient client = new HttpClient();
    Credentials creds = new UsernamePasswordCredentials("user", "pass");
    client.getState().setCredentials(AuthScope.ANY, creds);
    MyGetMethod method = new MyGetMethod(url goes here);
    client.executeMethod(method);
    if (method.isSuccess(method.getStatusCode())) {
        byte[] resp = method.getResponseBody();
        System.out.println("Got response: " + resp.length + " bytes");
    }
}