[Java] Java WebDAV client library Sardine

less than 1 minute read

Use Java’s simple WebDAV client library Sardine.

gradle

implementation'com.github.lookfirst:sardine:5.10'

When using 5.9 or earlier with Java 9 or later, it is necessary to explicitly add jaxb related dependencies.

compile group:'com.github.lookfirst', name:'sardine', version: '5.9'

// https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
compile group:'javax.xml.bind', name:'jaxb-api'

// https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core
compile group:'com.sun.xml.bind', name:'jaxb-core', version: '2.3.0.1'

// https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl
compile group:'com.sun.xml.bind', name:'jaxb-impl', version: '2.3.0.1'

If you do not do the above, the following ```NoClassDefFoundError

will occur. This is because Java 9 no longer loads jaxb by default.



```java
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at com.github.sardine.impl.SardineImpl.propfind(SardineImpl.java:459)

By the way, from 5.10, the dependency of ```org.glassfish.jaxb:jaxb-runtime

is included.



# java

```java
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;

import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import com.github.sardine.SardineFactory;

public class SardineSample {
public static void main(String[] args) throws IOException {
Sardine sardine = SardineFactory.begin();

// Get a list of resources
List<DavResource> resources = sardine.list("http://localhost:81/uploads/");
for (DavResource r :resources) {
System.out.println(r);
}

// Download
try (InputStream inputStream = sardine.get("http://localhost:81/uploads/asd.txt")) {
Files.copy(inputStream, Paths.get("copy.txt"), StandardCopyOption.REPLACE_EXISTING);
}

  //upload
sardine.put("http://localhost:81/uploads/asd222.txt", Files.readAllBytes(Paths.get("copy.txt")));
It's a sequel.
sardine.shutdown();
}
}

Tags:

Updated: