WebDAV(1)Introduction of WebDAV

WebDAV(1)IntroductionofWebDAV

1.configureintomcatprojecttostarttheserverpart

org.apache.catalina.servlets.WebdavServlet

Theconfigurationinweb.xml:

<servlet>

<servlet-name>webdav</servlet-name>

<servlet-class>org.apache.catalina.servlets.WebdavServlet</servlet-class>

<init-param>

<param-name>debug</param-name>

<param-value>0</param-value>

</init-param>

<init-param>

<param-name>listings</param-name>

<param-value>true</param-value>

</init-param>

<init-param>

<param-name>readonly</param-name>

<param-value>false</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>webdav</servlet-name>

<url-pattern>/*</url-pattern>

</servlet-mapping>

configurationinWebContent/META-INF/context.xml:

<Contextpath="/webapp-name"allowLinking="true">

</Context>

2.Learntheopen-sourceprojectjackrabbit

http://jackrabbit.apache.org

Therearebothserversideandclientsideofthisopen-sourceproject.

Imadeaverysimpleexampleofclientsideasfollow:

<dependency>

<groupId>org.apache.jackrabbit</groupId>

<artifactId>jackrabbit-jcr2dav</artifactId>

<version>2.4.1</version>

</dependency>

packagecom.sillycat.easywebdavclient.temprunner;

importjava.io.FileInputStream;

importjava.io.IOException;

importorg.apache.commons.httpclient.Credentials;

importorg.apache.commons.httpclient.HttpClient;

importorg.apache.commons.httpclient.UsernamePasswordCredentials;

importorg.apache.commons.httpclient.auth.AuthScope;

importorg.apache.commons.httpclient.methods.InputStreamRequestEntity;

importorg.apache.commons.httpclient.methods.RequestEntity;

importorg.apache.jackrabbit.webdav.DavConstants;

importorg.apache.jackrabbit.webdav.DavException;

importorg.apache.jackrabbit.webdav.MultiStatus;

importorg.apache.jackrabbit.webdav.MultiStatusResponse;

importorg.apache.jackrabbit.webdav.client.methods.DavMethod;

importorg.apache.jackrabbit.webdav.client.methods.MkColMethod;

importorg.apache.jackrabbit.webdav.client.methods.PropFindMethod;

importorg.apache.jackrabbit.webdav.client.methods.PutMethod;

publicclassTestJackrabbit{

publicstaticvoidmain(String[]args)throwsIOException,DavException{

HttpClientclient=newHttpClient();

//Credentialscreds=newUsernamePasswordCredentials("admin","admin");

//client.getState().setCredentials(AuthScope.ANY,creds);

DavMethodmkCol=newMkColMethod(

"http://localhost:8080/netstore-webdav/shared/testfolder/");

client.executeMethod(mkCol);

System.out.println("mkcoltestfolder:"+mkCol.getStatusCode()+""

+mkCol.getStatusText());

PutMethodput=newPutMethod(

"http://localhost:8080/netstore-webdav/shared/testfolder/1.txt");

RequestEntityrequestEntity=newInputStreamRequestEntity(

newFileInputStream("d://1.txt"));

put.setRequestEntity(requestEntity);

client.executeMethod(put);

System.out.println("putimagefile:"+put.getStatusCode()+""

+put.getStatusText());

DavMethodfind=newPropFindMethod(

"http://localhost:8080/netstore-webdav/shared/testfolder/",

DavConstants.PROPFIND_ALL_PROP,DavConstants.DEPTH_1);

client.executeMethod(find);

MultiStatusmultiStatus=find.getResponseBodyAsMultiStatus();

MultiStatusResponse[]responses=multiStatus.getResponses();

System.out.println("Foldersandfiles:");

for(inti=0;i<responses.length;i++){

System.out.println(responses[i].getHref());

}

}

}

3.Sardine

http://code.google.com/p/sardine/

ThisisaverysimpleclientAPI.

Imadeaverysimpleclientsidesampleasfollow:

<dependency>

<groupId>com.googlecode.sardine</groupId>

<artifactId>sardine</artifactId>

<version>314</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.6.4</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.6.4</version>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.16</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>1.6</version>

</dependency>

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

<version>4.2-beta1</version>

</dependency>

...snip...

<repositories>

<repository>

<id>sardine-google-svn-repo</id>

<snapshots><enabled>true</enabled></snapshots>

<name>SardinemavenrepoatGoogleCode</name>

<url>http://sardine.googlecode.com/svn/maven/</url>

</repository>

</repositories>

packagecom.sillycat.easywebdavclient.temprunner;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.util.List;

importcom.googlecode.sardine.DavResource;

importcom.googlecode.sardine.Sardine;

importcom.googlecode.sardine.SardineFactory;

publicclassTestSardine{

/**

*@paramargs

*@throwsIOException

*/

publicstaticvoidmain(String[]args)throwsIOException{

//Sardinesardine=SardineFactory.begin("admin","admin");

Sardinesardine=SardineFactory.begin();

sardine.disableCompression();

sardine.disablePreemptiveAuthentication();

if(sardine.exists("http://localhost:8080/netstore-webdav/shared/")){

System.out.println("sharedfolderexists");

}

if(!sardine.exists("http://localhost:8080/netstore-webdav/shared/testfolder/")){

sardine.createDirectory("http://localhost:8080/netstore-webdav/shared/testfolder/");

System.out.println("testfoldernotfolderexists");

}

InputStreamfis=newFileInputStream(newFile("d://1.txt"));

sardine.put("http://localhost:8080/netstore-webdav/shared/testfolder/2.txt",fis);

@SuppressWarnings("deprecation")

List<DavResource>resources=sardine.getResources("http://localhost:8080/netstore-webdav/shared/testfolder/");

for(DavResourceres:resources)

{

System.out.println(res);//callsthe.toString()method.

}

}

}

4.apachemodulemod_webdav

Iheardthatthereisamodulenamedmod_webdav,butIneedtobuildandconfiguretheserverside.

references:

http://code.google.com/p/sardine/

http://jackrabbit.apache.org/getting-started-with-apache-jackrabbit.html

http://www.artran.co.uk/articles/article/webdav-tomcat-apache/

http://www.tomcatexpert.com/knowledge-base/how-configure-tomcat-webdav

http://wiki.apache.org/jackrabbit/WebDAV

http://jackrabbit.apache.org/jackrabbit-configuration.html

http://jackrabbit.apache.org/first-hops.html

http://jackrabbit.apache.org/building-jackrabbit.html

http://jackrabbit.apache.org/source-repository.html

相关推荐