Sardine 下載 : https://github.com/lookfirst/sardine
Sardine Wiki : https://github.com/lookfirst/sardine/wiki/UsageGuide
Commons IO 的 FileUtils 下載 : http://commons.apache.org/proper/commons-io/download_io.cgi
FileUtils 教學:http://snkcxy.iteye.com/blog/1845862
import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import com.github.sardine.SardineFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class NewClass {
public static void main(String[] args) throws IOException {
Sardine sardine = SardineFactory.begin("username", "password");
download(sardine); // 下載
// upload(sardine); // 上傳
// delete(sardine); // 刪除
// resources(sardine); // 顯示該目錄所有檔案
// createDir(sardine); // 建立資料夾
sardine.shutdown();
}
static boolean isDownload;
static int count = 0;
static void download(Sardine sardine) throws IOException {
isDownload = true;
new Thread(() -> {
while (isDownload) {
try {
System.out.println(count / 1000 + " kb/s");
count = 0;
Thread.sleep(1000);
} catch (Exception e) {
}
}
}).start();
try (
InputStream is = sardine.get("http://www.example.com:80/loli/A.mp4");
FileOutputStream fos = new FileOutputStream("A.mp4");) {
byte buffer[] = new byte[8192];
for (int length = 0; (length = is.read(buffer)) != -1;) {
fos.write(buffer, 0, length);
count += length;
}
}
isDownload = false;
}
static void createDir(Sardine sardine) throws IOException {
sardine.createDirectory("http://www.example.com:80/loli/9999");
}
static void upload(Sardine sardine) throws IOException {
byte[] data = FileUtils.readFileToByteArray(new File("B.jpg"));
sardine.put("http://www.example.com:80/loli/C.jpg", data);
}
static void delete(Sardine sardine) throws IOException {
sardine.delete("http://www.example.com:80/loli/jails/");
}
static void resources(Sardine sardine) throws IOException {
List<DavResource> resources = sardine.list("http://www.example.com:80/loli");
for (DavResource res : resources) {
System.out.println(res);
}
}
}
留言列表