WebDAV客户端开发实例

本文介绍了如何开发WebDAV客户端,包括环境搭建、配置Slide服务器、配置客户端开发环境,以及提供了一个Java编码实例,展示了如何将本地文件上传到Slide服务器,并详细解释了代码流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; /* 336x280, 创建于 07-11-21 */ google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> (一)环境搭建 需从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("/nFilePath is:" path "/n"); //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("/n文件不存在,不需锁定.../n"); } 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/看看上传的文件。 由于页面编辑不方便,格式不太整齐,请原谅。 baozhengw999@hotmail.com succ100@163.com QQ:29803446
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值