zoukankan      html  css  js  c++  java
  • WebDav的java客户端开发包:sardine

    最近需要对WebDav服务器进行操作,查找了一下,基于java的开发包主要有这几个:
    slide
    Jackrabbit
    sardine
    webdavclient4j
    其中slide是apache的一个老的项目,url是http://jakarta.apache.org/slide/,这个
    项目已经退休了,在它的项目主页上推荐使用Jackrabbit项目。
    Jackrabbit是一个jcr实现,其中包括WebDav的服务器端和客户端。
    webdavclient4j我没看,因为我先找到了sardine,项目主页是http://code.google.com/p/sardine/,
    它最大的特点就是简单,使用非常简单,但项目主页也说了它并不是一个全面的实
    现,它只实现了WebDav的常用命令。

    下面是我在DAY的CQ5服务器(基于Jackrabbit)上的试验代码,代码创建了一个目录,然后
    向这个目录上传了一个jpg文件,最后列出了这个目录的内容。
    其中http://host:4502/crx/repository/crx.default是WebDav服务器的根路径,代码非常
    简单,唯一需要注意的是路径为目录时,记着最后要有“/”。

    Java代码  收藏代码
      1. package xxx;  
      2.   
      3. import java.io.File;  
      4. import java.io.FileInputStream;  
      5. import java.io.FileNotFoundException;  
      6. import java.io.InputStream;  
      7. import java.util.List;  
      8.   
      9. import com.googlecode.sardine.DavResource;  
      10. import com.googlecode.sardine.Sardine;  
      11. import com.googlecode.sardine.SardineFactory;  
      12. import com.googlecode.sardine.util.SardineException;  
      13.   
      14. public class TestSardine {  
      15.   
      16.     /** 
      17.      * @param args 
      18.      * @throws SardineException  
      19.      * @throws FileNotFoundException  
      20.      */  
      21.     public static void main(String[] args) throws SardineException, FileNotFoundException {  
      22.         Sardine sardine = SardineFactory.begin("admin", "admin");  
      23.           
      24.         if (sardine.exists("http://192.168.1.71:4502/crx/repository/crx.default/content/dam/")) {  
      25.             System.out.println("/content/dam folder exists");  
      26.         }  
      27.           
      28.         sardine.createDirectory("http://192.168.1.71:4502/crx/repository/crx.default/content/dam/testfolder/");  
      29.           
      30.         InputStream fis = new FileInputStream(new File("img12.jpg"));  
      31.         sardine.put("http://192.168.1.71:4502/crx/repository/crx.default/content/dam/testfolder/img12.jpg", fis);  
      32.           
      33.         List<DavResource> resources = sardine.getResources("http://192.168.1.71:4502/crx/repository/crx.default/content/dam/testfolder/");  
      34.         for (DavResource res : resources)  
      35.         {  
      36.              System.out.println(res); // calls the .toString() method.  
      37.         }  
      38.     }  
  • 相关阅读:
    OS模块
    利用一个random模块生成一个随机验证码功能
    random模块
    模块2
    模块module
    Java笔记汇总
    学习路上——技术书籍摸爬滚打
    web前端知识汇总——持续更新
    Python之路——进入Python学习
    Python细节备忘——时常拾遗以及关键点
  • 原文地址:https://www.cnblogs.com/xgjblog/p/3831052.html
Copyright © 2011-2022 走看看