2

我正在使用下面使用 sardine 的 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;
}
4

1 回答 1

1

你需要使用sardine.get()方法。方法文档 不要忘记使用文件的绝对路径。例如: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";
    }
}
于 2018-01-25T16:15:57.077 回答