WebDAV客户端开发实例

来源:互联网 发布:淘宝任务发布平台 编辑:程序博客网 时间:2024/06/02 05:26
(一)环境搭建
需从http://archive.apache.org/dist/jakarta/slide/binaries/下载jakarta-slide- 2.1-tomcat-5.0.28.zip,jakarta-slide-webdavclient-bin-2.1.zip,jakarta-slide -webdavclient-bin-2.1.zip如过需要源码也可以从http: //archive.apache.org/dist/jakarta/slide/source/找到server端和客户端的源码,相应文件是 jakarta-slide-server-src-2.1.zip,jakarta-slide-webdavclient-src-2.1.zip.
(二)配置slide server
其实解压jakarta-slide-2.1-tomcat-5.0.28.zip后启动tomcat就可以了,因为这个zip文件已经配置好了 slide server,在webapp目录下有一个slide.war,也可以拿这个文件在weblogic下配置一个slide server。如果机器上已配置好jdk,启动tomcat后,从浏览器地址栏输入http://localhost:8080/slide,这时会弹出 一个登录窗口,输入用户名root,口令root(用户名口令和角色的配置见slide.war中的Domail.xml和web.xml)即可。
(三)配置WebDAV客户端开发环境
开发WebDAV客户端需要在classpath中设置几个jar文件,从jakarta-slide-webdavclient-bin- 2.1.zip找到WebDAV客户端必须的四个jar文件:commons-httpclient.jar;jakarta-slide- webdavlib-2.1.jar;commons-logging.jar;jdom-1.0.jar。
(四)编码实例-向slide server上传文件:
slide server存储的文件位于tomcat bin目录下的store,经测试,如果需要将tomcat
slide 中的文件转移到weblogic下部署的slide,只需将store目录整个复制即可。下面是一个WebDAV客户端文件上传代码:
//我将自己写的客户端类添加到了jakarta-slide-webdavlib-2.1.jar里,路径是
//org.apache.webdav.lib.mycom

package org.apache.webdav.lib.mycom;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpURL;
import org.apache.webdav.lib.WebdavResource;
import java.util.*;
...
//下面是putFileToWebDAV方法:
public class CenSlideClientImpl{

public static final boolean putFileToWebDAV(String urlPath,String urlFileName,String uid,String pwd,String localPath,String localFileName)
{
boolean bool = false;
try
{

HttpURL hrl = new HttpURL(urlPath);
hrl.setUserinfo(uid,pwd);
WebdavResource wdr = new WebdavResource(hrl);
System.out.println("测试Web路径:" +wdr.getPath());

File file = new File(localPath+"/"+localFileName); //指定上传本地某个目录下的文件
String path = wdr.getPath();
if(!path.endsWith("/"))
path +="/";
path += urlFileName; //上传到WebDAV后可按另外一个文件名保存
System.out.println(" FilePath is:" +path+" ");
//if (!wdr.isCollection())
// throw new Exception("Path is currently a file");
//wdr.setPath(currentPath + "/" + urlFileName);
//wdr.setPath("/webdav/down.txt"); //如果指定的文件不存在则发生异常。

try //尝试锁定
{
wdr.setPath(path);

if (wdr.isLocked())
{

// bool = false;
// System.out.println("文件已被锁定,上传失败!");
// return bool;
wdr.unlockMethod();
bool = wdr.putMethod( path,file );

}
else
{
// wdr.lockMethod(uid,10000000);
bool = wdr.putMethod( path,file );
//wdr.unlockMethod();
}

}
catch(Exception ex)
{
bool = wdr.putMethod( path,file );
//wdr.unlockMethod();
System.out.println(" 文件不存在,不需锁定... ");
}
finally
{

wdr.close(); 

}
}
catch(MalformedURLException mue)
{

System.out.println("MalformedURLException:"+mue.getMessage());
}
catch(HttpException he)
{

System.out.println("HttpException:"+he.getMessage());

}
catch(IOException ioe)
{

System.out.println("IOException:"+ioe.getMessage());


catch(Exception ex)
{

System.out.println("ThrowException:"+ex.getMessage());
}
finally
{
//wdr.close();
}

return bool;


}

public static void main (String args[]) 
{
boolean bool = false;
try{
bool = putFileToWebDAV("http://localhost:8080/slide/files/","remotefile.txt","root","root","c:/","localfile.txt");
}
catch(Exception ex){}
}

}
main 方法里的调用说明:将本地c:localfile.txt文件上传到http://localhost:8080/slide/files/并命名为 remotefile.txt,连接slide server的用户名和口令分别为root和root,这时再打开浏览器
http://localhost:8080/slide/files/看看上传的文件。
0 0
原创粉丝点击