首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用沙丁鱼从webdav服务器下载zip文件?

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

Stack Overflow用户
提问于 2016-09-29 00:46:32
回答 1查看 1.4K关注 0票数 2

我正在使用下面的java类,它使用沙丁鱼,我在目录中只得到资源或zip文件列表,我应该使用什么来下载zip文件?

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2018-01-26 00:15:57

您需要使用sardine.get()方法。Method documentation不要忘记使用文件的绝对路径。例如:http://hfus.com/vsd/file.zip

代码示例:

代码语言:javascript
复制
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";
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39753320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档