Is it possible to write files to a remote directory using java?

joe picture joe · Sep 9, 2009 · Viewed 13.5k times · Source

I have the directory mapped on my machine so that I can browse and write to it via Windows explorer. I would like to write files via java.

File f = new File("http://dev1:8080/data/xml/myTestFile123.xml");

f.createNewFile();

I am getting the following error:

Exception in thread "main" java.io.IOException: The filename, directory name, or volume label syntax is incorrect
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(Unknown Source)
    at MainTest.createTestFile(MainTest.java:156)
    at MainTest.main(MainTest.java:72)

Is there any way to write files to a mapped directory that has the http:// in front? Because thats the way the directory is provided to me. It is a virtual directory that an oracle database is creating.

Answer

Pascal Thivent picture Pascal Thivent · Sep 10, 2009

My understanding is that you are trying to write to an Oracle XML DB Repository. Oracle XML DB Repository is a feature that has been introduced by Oracle9i Database Release 2 for XML storage and that can be accessed through FTP or HTTP/WebDAV. In your case, it looks like you're trying to use HTTP/WebDAV.

As explained in the WedDAV page on Wikipedia:

WedDAV is a set of extensions on top of HTTP that allows users to edit and manage files collaboratively on remote World Wide Web servers.

In other words, adding files, deleting them, renaming them, etc in a WebDAV repository is done using HTTP words: PUT, DELETE, MOVE, etc (see RFC 4918 for more details).

Consequently, interacting with a WebDAV server can be done using classes from java.net.

Or you could use a higher level API like Jakarta Commons HttpClient.

Or you could use a Java WebDAV client like the one provided by the Slide project. This article shows how to do it and it looks simple. However, as the Slide project is now retired, I wouldn't recommend it.

Luckily (or not), the Apache Jackrabbit project is an alternative to Slide... but AFAIK the WebDAV support in Jackrabbit is more focused on server-side implementations than clients. Anyway, you'll find some code samples in this thread on the jackrabbit-users mailing list.

I think I'd choose HttpClient and use the Tutorial or the Sample Code as starting points.