Java's WebDAV client library Sardine

Use Java's simple WebDAV client library Sardine.

gradle

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'

// 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 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(SardineImpl.java:459)

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

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 resource list
		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")));
		
		sardine.shutdown();
	}
}

Recommended Posts

Java's WebDAV client library Sardine