title: Upload file to eXist-db using Java
body: <p>I'm writing an application which allows for uploading a file to a specific collection in eXist-db. I am basing this application off <a href="http://exist-db.org/exist/devguide_xmldb.xml;jsessionid=1btqxaaw7ekzjnsgyya4v75ch" rel="nofollow">code I found on the eXist web site</a>.</p>
  
  <p>Unfortunately, the code does not seem to work - when I test it an error message appears</p>
  
  <pre><code>usage: StoreExample collection-path document
  </code></pre>
  
  <p>When I change the URI <em>xmldb:exist://localhost:8080/exist/xmlrpc</em> to <em>http://localhost:8080/exist/admin/admin.xql;jsessionid=1fkd05vvfv6kq</em> and the collection to <em>/db/col1</em>, the following error occurs: </p>
  
  <pre><code> 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 ==&gt; Collection col =DatabaseManager.getCollection(URI + collection);
  </code></pre>
  
  <p>Here is my code: </p>
  
  <pre><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.");
     }
  }
  </code></pre>
  
  <p>All help appreciated,thanks.</p>