论坛首页 Java企业应用论坛

WebDav的java客户端开发包:Jackrabbit

浏览 4889 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-01-25  
上一篇帖子“WebDav的java客户端开发包:sardine”中说到,对于开发WebDav客户端
sardine是一个很好的选择,但sardine并未实现WevDav的全部规范,所以我又试了试
apache的Jackrabbit,这个是已退休的slide的替代品。
Jackrabbit中的WevDav客户端只是一个附带的模块,并不是Jackrabbit的主要功能,
所以关于开发的文档很少,我只找到一篇:
http://wiki.apache.org/jackrabbit/WebDAV
我是下载的jackrabbit-standalone-2.0.3.jar包,其中应该大部分类都用不到,不过
我没找到单独的WebDav客户端的包。

演示程序的主要处理就是创建了一个目录,然后上载了jpg文件,最后列出了创建目录
中的内容。其中创建目录要用MkCol命令,上载用Put命令,列出目录内容其实是列出
指定路径下的所有属性。我没有仔细研究过WebDav的规范,估计jackrabbit是按照规范
实现的,需要理解WebDav的规范才能很好地使用,sardine则要接近对WebDav的一般性
理解,不需要理解相应的规范就可以快速地开始使用。

下面就是我的演示程序代码:

package test;

import java.io.FileInputStream;
import java.io.IOException;

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.DavConstants;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;

public class TestJackrabbit {
	public static void main(String[] args) throws IOException, DavException {
		HttpClient client = new HttpClient();
		Credentials creds = new UsernamePasswordCredentials("admin", "admin");
		client.getState().setCredentials(AuthScope.ANY, creds);

		DavMethod mkCol = new MkColMethod("http://192.168.1.71:4502/crx/repository/crx.default/content/dam/testfolder");
		client.executeMethod(mkCol);
		System.out.println("mkcol testfolder:" + mkCol.getStatusCode() + " " + mkCol.getStatusText());

		PutMethod put = new PutMethod("http://192.168.1.71:4502/crx/repository/crx.default/content/dam/testfolder/testimg.jpg");
		RequestEntity requestEntity = new InputStreamRequestEntity(new FileInputStream("img12.jpg"));
		put.setRequestEntity(requestEntity);
		client.executeMethod(put);
		System.out.println("put image file:" + put.getStatusCode() + " " + put.getStatusText());
		
		DavMethod find = new PropFindMethod("http://192.168.1.71:4502/crx/repository/crx.default/content/dam/testfolder", DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
	    client.executeMethod(find);
	    MultiStatus multiStatus = find.getResponseBodyAsMultiStatus();
	    MultiStatusResponse[] responses = multiStatus.getResponses();
	    System.out.println("Folders and files:");
	    for (int i=0; i<responses.length; i++) {
            System.out.println(responses[i].getHref());
	    }  
	}
}

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics