2016-09-28 71 views
1

我正在使用下面的java類,它使用沙丁魚,我只獲取目錄中的資源或zip文件列表,我應該使用什麼來下載zip文件?如何使用沙丁魚從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

你找到了嗎?我發現commons 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"; 
    } 
} 
相關問題