WebDAV用戶端開發執行個體

來源:互聯網
上載者:User
(一)環境搭建
需從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/看看上傳的檔案。

由於頁面編輯不方便,格式不太整齊,請原諒。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.