Java 如何使用sardine从webdav服务器下载zip文件?
我正在使用下面的java类,它使用沙丁鱼,我只获得目录中的资源或zip文件列表,我应该使用什么来下载zip文件Java 如何使用sardine从webdav服务器下载zip文件?,java,download,mule,webdav,sardine,Java,Download,Mule,Webdav,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.Sardi
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;
}
package.com下载;
导入java.util.List;
导入org.mule.api.MuleEventContext;
导入org.mule.api.lifecycle.Callable;
导入com.github.sardine.DavResource;
进口com.github.sardine.sardine;
进口com.github.sardine.SardineFactory;
公共类FileDownload实现了可调用{
@凌驾
公共对象onCall(MuleEventContext eventContext)引发异常{
沙丁鱼沙丁鱼=沙丁鱼工厂。开始(“***”,“***”);
列表资源=沙丁鱼。列表(“http://hfus.com/vsd");
for(资源:资源)
{
系统输出打印项次(res);
}
返回沙丁鱼;
}
您需要使用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";
}
}
package.com下载;
导入java.util.List;
导入org.mule.api.MuleEventContext;
导入org.mule.api.lifecycle.Callable;
导入com.github.sardine.DavResource;
进口com.github.sardine.sardine;
进口com.github.sardine.SardineFactory;
//TODO:添加缺少的导入
公共类FileDownload实现了可调用{
@凌驾
公共对象onCall(MuleEventContext eventContext)引发异常{
沙丁鱼沙丁鱼=沙丁鱼工厂。开始(“***”,“***”);
List resources=sardine.List(serverUrl()+“/vsd”);
for(资源:资源){
if(res.getName().endsWith(“.zip”)){
下载文件(res);
}
}
返回沙丁鱼;
}
私有void下载文件(DavResource){
试一试{
InputStream in=sardine.get(serverUrl()+resource.getPath());
//TODO:在子目录中处理相同的文件名
OutputStream out=新文件OutputStream(resource.getName());
IOUtils.copy(输入、输出);
in.close();
out.close();
}捕获(IOEX异常){
//TODO:处理异常
}
}
私有字符串serverUrl(){
返回“http://hfus.com";
}
}
你已经找到什么了吗?我发现commons vfs允许zip和webdav