如何使用WebDAV更新服务器资源

时间:2015-07-03 10:26:37

标签: android webdav ithit-webdav-server

我正在编写一个Android应用程序,我正在使用Jackrabbit WebDAV Library。

借助此WebDAV库,我可以从服务器下载和上传文件。这可以按预期工作。

现在我想从文件中获取一个具有来自服务器的特定偏移量的字节数组。为此,我试过的是:

byte buffer[] = null;
Log.d(TAG, "downloadBytes '" + fileUri + "'  byteOffset '" + byteOffset + "'  byteOffset '" + byteCount);
HttpClient httpClient = new HttpClient();

GetMethod httpMethod = new GetMethod(fileUri);

httpMethod.addRequestHeader("Accept-Ranges", "bytes");
int total = byteOffset + byteCount;
String bytesMessage = "bytes=" + byteOffset + "-" + total;
Log.d(TAG, "bytesMessage " + bytesMessage);

httpMethod.addRequestHeader("Range", bytesMessage);
httpMethod.addRequestHeader("Cache-Control", "no-cache, no-store");

httpClient.executeMethod(httpMethod);

这可以按预期工作。

同样,我想通过指定offset来更新服务器上带有字节数组的文件,但这似乎不起作用;服务器返回400 Bad Request错误代码。

byte buffer[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50};
Log.d(TAG, "downloadBytes '" + fileUri + "'  byteOffset '" + byteOffset + "'  byteCount '" + byteCount);
HttpClient httpClient = new HttpClient();


PutMethod httpMethod = new PutMethod(fileUri);

httpMethod.addRequestHeader("Accept-Ranges", "bytes");
int total = byteOffset + byteCount;
String bytesMessage = "bytes=" + byteOffset + "-" + total;
Log.d(TAG, "bytesMessage " + bytesMessage);

httpMethod.addRequestHeader("Range", bytesMessage);
httpMethod.addRequestHeader("Cache-Control", "no-cache, no-store");
httpMethod.addRequestHeader("Overwrite", "T");

InputStream is = new ByteArrayInputStream(buffer);
RequestEntity requestEntity = new InputStreamRequestEntity(is, "text/plain");
httpMethod.setRequestEntity(requestEntity);

httpClient.executeMethod(httpMethod);

请求标题中缺少什么?

1 个答案:

答案 0 :(得分:0)

以下是正确的标头,使用此标题我可以使用偏移和字节长度更新webdav服务器资源。

        HttpClient httpClient = new HttpClient();

        PutMethod httpMethod = new PutMethod(fileUri);
        httpMethod.addRequestHeader("Accept-Ranges", "bytes");
        int total = byteOffset + byteCount;
        int range = total-1;

        String rangeMessage = "bytes=" + byteOffset +"-" + range;
        Log.d(TAG, "rangeMessage " + rangeMessage);

        String lengthMessage = Integer.toString(byteCount);
        String contentRangeMessage = "bytes " + byteOffset +"-" + range + "/*";

        Log.d(TAG, "contentRangeMessage " + contentRangeMessage);
        Log.d(TAG, "lengthMessage " + lengthMessage);

        httpMethod.addRequestHeader("Content-Length", lengthMessage);
        httpMethod.addRequestHeader("Content-Range", contentRangeMessage);
        httpMethod.addRequestHeader("Cache-Control", "no-cache, no-store");
        httpMethod.addRequestHeader("Overwrite", "T");