7

my ajax application uploads a file to a Java application container from the user's browser. What I'd like to do is this: once the uploading has completed I want to "send" the file to a WebDAV server, identified by the host name (i.e. localhost), the port (i.e. 8080) and the location where I want to store the file (i.e. dir1/dir2).

What I am after is basically a WebDAV client framework that enables me to upload a file to WebDAV. In my application I am already using "webdavclient4j", but I don't seem to find a way to upload a file with it?

Any ideas? Thanks in advance for any help you might provide.

F

francescoNemesi
  • 133
  • 1
  • 1
  • 5

2 Answers2

14

You can do it with only a few lines of code using my recently released and super easy to use modern webdav client for java, Sardine. Here is a couple examples (first one uses commons-io to read the file):

Sardine sardine = SardineFactory.begin("username", "password");
byte[] data = FileUtils.readFileToByteArray(new File("/file/on/disk"));
sardine.put("http://yourdavserver.com/adirectory/nameOfFile.jpg", data);

or using streams:

Sardine sardine = SardineFactory.begin("username", "password");
InputStream fis = new FileInputStream(new File("/some/file/on/disk.txt"));
sardine.put("http://yourdavserver.com/adirectory/nameOfFile.jpg", fis);

https://github.com/lookfirst/sardine

cheers,

jon

Public Profile
  • 1,817
  • 1
  • 21
  • 20
  • Hi Jon, thanks for your response. I tried Sardine and decided to use it in my applications. Makes things much easier. F – francescoNemesi Feb 22 '10 at 08:26
  • I too implemented Sardine. Amazingly simple to create a webservice from -- now I just have to get it all tested ;) – Bill Mote Jun 22 '11 at 00:55
  • @Jon: Is there a way to directly upload a directory? And if there is a directory present on the webserver, would it just overwrite the files? – rkg Nov 03 '11 at 17:51
  • Uploading an entire directory is the same as uploading a bunch of individual files. Sardine allows you to both upload individual files as well as check to see if something is present on the server. – Public Profile Nov 04 '11 at 00:10
  • Hi Jon, I need to upload documents using a SSL certificate, I read the wiki but I'm not able to figure how to link between KeyStore and SSLSocketFactory https://github.com/lookfirst/sardine/wiki/UsageGuide#ssl – Abderrazak BOUADMA Nov 07 '14 at 16:03
  • Sardine API looks pretty friendly, but it fails to work with a particular Oracle server (with error like no response from server). Although web browsers and Apache Jackrabbit can handle it. – Pavel Vlasov Dec 03 '15 at 10:06
  • I could not create a new directory with sardine, createdirectory uses MKCOL verb but it does not work. is there another way to create a directory? – Orhun D. Sep 02 '16 at 14:10
7

You can use the Jackrabbit WebDAV Library.

An example of a WebDAV client to upload content to a WebDAV server (taken from here):

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;

...

// WebDAV URL:
final String baseUrl = ...;
// Source file to upload:
File f = ...;
try{
    HttpClient client = new HttpClient();
    Credentials creds = new UsernamePasswordCredentials("username", "password");
    client.getState().setCredentials(AuthScope.ANY, creds);

    PutMethod method = new PutMethod(baseUrl + "/" + f.getName());
    RequestEntity requestEntity = new InputStreamRequestEntity(
        new FileInputStream(f));
    method.setRequestEntity(requestEntity);
    client.executeMethod(method);
    System.out.println(method.getStatusCode() + " " + method.getStatusText());
}
catch(HttpException ex){
    // Handle Exception
}
catch(IOException ex){
    // Handle Exception
}
Desintegr
  • 6,992
  • 1
  • 22
  • 17
  • I tried and tried to get the code working you have posted. It caused no end of grief with Tomcat. Our WebDAV server is based on Jackrabbit so I really wanted that to work. – Bill Mote Jun 22 '11 at 00:54
  • Thanks! It helped me with an Oracle webdav server (unlike Sardine). – Pavel Vlasov Dec 03 '15 at 08:59