Java's WebDAV client library Sardine

Use Java's simple WebDAV client library Sardine.


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

If you want to use 5.9 or earlier with Java 9 or later, you need to explicitly add jaxb related dependencies.

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

compile group: 'javax.xml.bind', name: 'jaxb-api'

compile group: 'com.sun.xml.bind', name: 'jaxb-core', version: ''

compile group: 'com.sun.xml.bind', name: 'jaxb-impl', version: ''

If you do not do the above, the following `` `NoClassDefFoundError``` will occur. This is because jaxb is no longer loaded by default in Java 9.

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

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


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 resource list
		List<DavResource> resources = sardine.list("http://localhost:81/uploads/");
		for (DavResource r : resources) {

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

		sardine.put("http://localhost:81/uploads/asd222.txt", Files.readAllBytes(Paths.get("copy.txt")));

Recommended Posts

Java's WebDAV client library Sardine