简体   繁体   English

如何使用沙丁鱼从webdav服务器下载zip文件?

[英]How to download zip files from webdav server using sardine?

I am using below java class which uses sardine , i am getting only resources or zip files list in the directory, what should i use to download zip files? 我正在使用下面使用沙丁鱼的java类,我只获取目录中的资源或zip文件列表,我应该用什么来下载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;
}

You need to use sardine.get() method. 您需要使用sardine.get()方法。 Method documentation Don't forget to use absolute path to your file. 方法文档不要忘记使用文件的绝对路径。 For example: http://hfus.com/vsd/file.zip . 例如: http://hfus.com/vsd/file.ziphttp://hfus.com/vsd/file.zip

Code sample: 代码示例:

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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM