I'm writing an application which allows for uploading a file to a specific collection in eXist-db. I am basing this application off code I found on the eXist web site.
Unfortunately, the code does not seem to work - when I test it an error message appears
usage: StoreExample collection-path document
When I change the URI xmldb:exist://localhost:8080/exist/xmlrpc to http://localhost:8080/exist/admin/admin.xql;jsessionid=1fkd05vvfv6kq and the collection to /db/col1, the following error occurs:
Exception in thread "main" org.xmldb.api.base.XMLDBException:
at org.xmldb.api.DatabaseManager.getDatabase(Unknown Source)
at org.xmldb.api.DatabaseManager.getCollection(Unknown Source)
at org.xmldb.api.DatabaseManager.getCollection(Unknown Source)
at addingfiletest.exp.main(exp.java:44)
ligne 44 ==> Collection col =DatabaseManager.getCollection(URI + collection);
Here is my code:
import java.io.File;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;
public class exp {
public final static String URI = "http://localhost:8080/exist/admin/admin.xql;jsessionid=1fkd05vvfv6kq";
public static void main(String args[]) throws Exception {
String collection = "/db/col1", file = "D:/PFE/lien.txt";
// initialisation du driver
String driver = "org.exist.xmldb.DatabaseImpl";
Class cl = Class.forName(driver);
Database database = (Database) cl.newInstance();
DatabaseManager.registerDatabase(database);
// Accès à la collection
Collection col = DatabaseManager.getCollection(URI + collection);
// créer une nouvelle XMLResource; un id sera affecté à la nouvelle
// ressource
XMLResource document = (XMLResource) col.createResource(null,
"XMLResource");
File f = new File(file);
if (!f.canRead()) {
System.out.println("cannot read file " + file);
return;
}
document.setContent(f);
System.out.print("storing document " + document.getId() + "...");
col.storeResource(document);
System.out.println("ok.");
}
}
All help appreciated,thanks.