2016-09-28 42 views
1

私はSardineを使用するjavaクラスを使用しています。私はディレクトリ内のリソースまたはzipファイルのリストのみを取得していますが、zipファイルをダウンロードするにはどうすればよいですか?Sardineを使ってwebdavサーバーからzipファイルをダウンロードするには?

package com.download; 
import java.util.List; 

import org.mule.api.MuleEventContext; 
import org.mule.api.lifecycle.Callable; 
import com.github.sardine.DavResource; 
import com.github.sardine.Sardine; 
import com.github.sardine.SardineFactory; 

public class filesdownload implements Callable{ 

@Override 
public Object onCall(MuleEventContext eventContext) throws Exception { 
    Sardine sardine = SardineFactory.begin("***","***"); 

    List<DavResource> resources = sardine.list("http://hfus.com/vsd"); 
    for (DavResource res : resources) 
    { 
     System.out.println(res); 
    } 

    return sardine; 
} 
+0

あなたはすでに何かを見つけましたか?私はコモンズを見つけたvfsはzipとwebdavを許可する – codesmith

答えて

0

sardine.get()メソッドを使用する必要があります。 Method documentation ファイルへの絶対パスを使用することを忘れないでください。例:http://hfus.com/vsd/file.zip

コードサンプル:

package com.download; 
import java.util.List; 

import org.mule.api.MuleEventContext; 
import org.mule.api.lifecycle.Callable; 
import com.github.sardine.DavResource; 
import com.github.sardine.Sardine; 
import com.github.sardine.SardineFactory; 
//TODO: add missing imports 

public class filesdownload implements Callable{ 

    @Override 
    public Object onCall(MuleEventContext eventContext) throws Exception { 
     Sardine sardine = SardineFactory.begin("***","***"); 

     List<DavResource> resources = sardine.list(serverUrl()+"/vsd"); 
     for (DavResource res : resources) { 
      if(res.getName().endsWith(".zip")) { 
       downloadFile(res); 
      } 
     } 

     return sardine; 
    } 

    private void downloadFile(DavResource resource) { 
     try { 
      InputStream in = sardine.get(serverUrl()+resource.getPath()); 
      // TODO: handle same file name in subdirectories 
      OutputStream out = new FileOutputStream(resource.getName()); 
      IOUtils.copy(in, out); 
      in.close(); 
      out.close(); 
     } catch(IOException ex) { 
      // TODO: handle exception 
     } 
    } 

    private String serverUrl() { 
     return "http://hfus.com"; 
    } 
} 
関連する問題