Article:
  Sliding into WebDAV
Subject:   Sample Working WebDAV code
Date:   2007-01-09 10:49:05
From:   fayazkm
I have some working code here that might be of help. I had an invocation to the delete method that always returned false but i believe that is because the WebDAV server does not support the delete method. I will post an update when i get it to work. The following code will list, download and upload files.


private void pickup() throws Exception {


String url = "https://vnet.visa.com.ar/webdav/Bandeja de Entrada/HOMOLOGACIONVASA/";
HttpsURL hrl = new HttpsURL(url);
// webdav server login username & password
hrl.setUserinfo("username", "password");
System.out.println("User/Password set.");
Credentials cred = new UsernamePasswordCredentials("proxyuser", "proxypassword");
wdr = new WebdavResource(hrl, "proxyaddr", 443, cred);
wdr.setDebug(3);
String wdrpath = wdr.getPath();
WebdavResource wdrfile = null;
HttpsURL hrlfile = null;



// get list of files
String files[] = listFiles();


// for downloading
for (int i = 0; i < files.length; i++) {


System.out.println(files[i]);


if (files[i].indexOf("A_PPI_modificacion_periodos.txt") != -1) {
File f = new File("C:/" + files[i]);


hrlfile = new HttpsURL(url + files[i]);
hrlfile.setUserinfo("username", "password");
wdrfile = new WebdavResource(hrlfile, "proxyaddress", 443, cred);
wdrfile.getMethod(f);
System.out.println("wdrfile.getStatusCode()="+wdrfile.getStatusCode());
System.out.println("wdrfile.getStatusMessage()="+wdrfile.getStatusMessage());
long time = wdrfile.getGetLastModified();
System.out.println(time);
Date date = new Date(time);
System.out.println(date + "|||||" + date.getTime());
System.out.println("File " + files[i] + " downloaded.");
//boolean deleted = wdrfile.deleteMethod();
//System.out.println("wdrfile.getStatusCode()="+wdrfile.getStatusCode());
//System.out.println("wdrfile.getStatusMessage()="+wdrfile.getStatusMessage());
//System.out.println("File deletion returned "+deleted);
wdrfile.close();
}


}
wdr.close();
}



public String[] listFiles() throws Exception {
if (!wdr.isCollection())
throw new Exception("Path is currently a file");
return wdr.list();
}



private void dropOff() throws IOException {
// another way for uploading


HttpsURL hrl = new HttpsURL("url");
System.out.println("HttpsURL created.");
hrl.setUserinfo("username", "passowrd");


System.out.println("User/Password set.");


Credentials cred = new UsernamePasswordCredentials("proxyuserid", "proxypassword");
wdr = new WebdavResource(hrl, "proxyaddress", 443, cred);
wdr.setDebug(3);


System.out.println("WebdavResource created.");
String wdrpath = wdr.getPath();


String filename = "A_Test_1.txt";
String file = "C:/" + filename;


File f = new File(file);
System.out.println("File object created.");
String path = wdrpath + filename;
System.out.println("wdrpath=" + wdrpath);
System.out.println("path=" + path);
boolean rslt = wdr.putMethod(path, f);
System.out.println("Result of upload is " + rslt);



wdr.close();


}


Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Sample Working WebDAV code
    2008-07-03 00:26:13  AACR [View]

    Thanks so much, it was helpfully for me.
    We need more people like you!!
    Sorry for my English.
  • Sample Working WebDAV code
    2007-06-22 05:07:12  kevnyang [View]

    Very good. I am not quite sure what is the proxyaddress? Is it possible to send me the complete code,please?