解壓 Server 包後,直接啟動內建 Tomcat(bin/startup.sh 或 .bat)。瀏覽器訪問 https://localhost:8080/slide,輸入預設帳號 root/root 即可登錄(帳號/角色定義於 slide.war 內的 Domain.xml 與 web.xml)。
若需部署至 WebLogic,請參考上一篇《Apache Slide 部署至 WebLogic 環境完整指南》。
從 Client 包中提取以下 4 個核心 JAR 至項目 Classpath:
commons-httpclient.jarjakarta-slide-webdavlib-2.1.jarcommons-logging.jarjdom-1.0.jarSlide Server 的文件實際存儲於 Tomcat bin/store 目錄。若需將 Tomcat 部署的資料遷移至 WebLogic 部署的 Slide,僅需將整個 store 目錄複製過去即可。
以下封裝了鎖定檢查、自動解鎖、異常處理的上傳工具類,已整理為可直接使用的完整代碼:
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.*;
public class CenSlideClientImpl {
/**
* 向 Slide Server 上傳文件
* @param urlPath Server 路徑,如 "https://localhost:8080/slide/files/"
* @param urlFileName 上傳後在 Server 的文件名
* @param uid 登錄用戶名
* @param pwd 登錄密碼
* @param localPath 本地文件目錄
* @param localFileName 本地文件名
* @return 上傳是否成功
*/
public static boolean putFileToWebDAV(String urlPath, String urlFileName,
String uid, String pwd,
String localPath, String localFileName) {
boolean success = false;
WebdavResource wdr = null;
try {
HttpURL hrl = new HttpURL(urlPath);
hrl.setUserinfo(uid, pwd);
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;
System.out.println("\n目標路徑: " + path + "\n");
wdr.setPath(path);
// 嘗試鎖定處理
try {
if (wdr.isLocked()) {
System.out.println("文件已被鎖定,嘗試解鎖...");
wdr.unlockMethod();
} else {
// 可選:預先鎖定防止併發衝突
// wdr.lockMethod(uid, 10000000);
}
success = wdr.putMethod(path, file);
} catch (Exception ex) {
// 文件不存在等情況直接上傳
System.out.println("\n文件不存在或無需鎖定,直接上傳...\n");
success = wdr.putMethod(path, file);
}
} catch (MalformedURLException mue) {
System.err.println("URL 格式錯誤: " + mue.getMessage());
} catch (HttpException he) {
System.err.println("HTTP 協議異常: " + he.getMessage());
} catch (IOException ioe) {
System.err.println("I/O 錯誤: " + ioe.getMessage());
} catch (Exception ex) {
System.err.println("未知異常: " + ex.getMessage());
} finally {
if (wdr != null) {
try { wdr.close(); } catch (IOException ignored) {}
}
}
return success;
}
// 測試主方法
public static void main(String[] args) {
boolean result = putFileToWebDAV(
"https://localhost:8080/slide/files/",
"remotefile.txt",
"root", "root",
"c:\\", "localfile.txt"
);
System.out.println("上傳結果: " + (result ? "成功" : "失敗"));
}
}
setUserinfo(uid, pwd) 設置 Basic Auth 憑證。/ 結尾,再拼接目標文件名。isLocked(),若被鎖定先 unlockMethod();實際生產中建議上傳前主動 lockMethod(),上傳後 unlockMethod(),防止併發覆蓋。putMethod 可能拋出的各類異常,確保資源釋放。finally 區塊確保 WebdavResource.close() 被調用,釋放連接。運行 main 方法後,瀏覽器重新訪問 https://localhost:8080/slide/files/,即可看到 remotefile.txt 已出現在列表中。
Apache Slide 提供了完整的 WebDAV 客戶端庫,配合 Commons HttpClient 可快速構建文件管理功能。實際項目中建議進一步封裝:加入重試機制、進度回調、大文件分塊上傳等增強特性。
作者聯繫方式:
MSN: baozhengw999@hotmail.com
Email: succ100@163.com
QQ: 29803446