0

我正在编写一个应用程序,它允许将文件上传到 eXist-db 中的特定集合。我将这个应用程序基于我在 eXist 网站上找到的代码

不幸的是,代码似乎不起作用 - 当我测试它时出现错误消息

usage: StoreExample collection-path document

当我将 URI xmldb:exist://localhost:8080/exist/xmlrpc更改为http://localhost:8080/exist/admin/admin.xql;jsessionid=1fkd05vvfv6kq并将集合更改为/db/col1时,出现以下错误发生:

 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);

这是我的代码:

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.");
   }
}

感谢所有帮助,谢谢。

4

2 回答 2

1

我过去创建了一个小型应用程序,我创建新集合的解决方案是:我使用 exists xqj 库(ex - exists-xqj-1.0.1.jar; ex - xqj2-0.0.1.jar ; ex - xqjapi.jar首先你需要创建连接:

private static void conectar() {
    try {
        xqc = crearconexion("localhost", "8080", "admin", "abc123.");
    } catch (XQException ex) {
        System.out.println("No hay conexion con la base de datos");
    }
}

    private static XQConnection crearconexion(String host, String puerto, String admin, String pass) throws XQException {
    xqjd = new ExistXQDataSource();
    xqjd.setProperty("serverName", host);
    xqjd.setProperty("port", puerto);
    xqjd.setProperty("user", admin);
    xqjd.setProperty("password", pass);
    xqc = xqjd.getConnection();
    return xqc;
}

(java)我有一个公开的方法在这里创建这个集合(.....)

    consulta = 
             "declare namespace exist = \"http://exist.sourceforge.net/NS/exist\"; \n"
            + "declare namespace request=\"http://exist-db.org/xquery/request\"; \n"
            + "    declare namespace xmldb=\"http://exist-db.org/xquery/xmldb\";  \n"
            + "    declare variable $file as xs:string { \""
            + "<menu>\n"
            + "    <maquina>\n"
            + "        <listname>"+maquina.getNombre()+"</listname>\n"
            + "    </maquina>\n"
            + "    <game name=\'0\'>\n"
            + "        <description></description>\n"
            + "        <cloneof/>\n"
            + "        <manufacturer></manufacturer>\n"
            + "        <personalizado>\n"
            + "            <estrellas>0</estrellas>\n"
            + "            <lotengo>No</lotengo>\n"
            + "            <jugado>No</jugado>\n"
            + "            <rutaimagen/>\n"
            + "        </personalizado>\n"
            + "        <rating></rating>\n"
            + "        <year></year>\n"
            + "        <genre></genre>\n"
            + "        <clave>1</clave>\n"
            + "    </game>\n"
            + "</menu>\""
            + " };  \n"
            + "    declare variable $name as xs:string { \""+maquina.getFile()+"\" };  \n"
            + "    declare variable $collection as xs:string { \""+RUTAROOT+"\" };  \n"
            + "      \n"
            + "    <results>  \n"
            + "    {  \n"
            + "    let $load-status := xmldb:store($collection, $name, $file)  \n"
            + "    return <load-status> { $load-status } </load-status>  \n"
            + "    }  \n"
            + "    </results>  ";

        ejecutarConsultaU(consulta);

private static void ejecutarConsultaU(String cadenaAConsultar) throws XQException {
    XQExpression consulta;
    consulta = xqc.createExpression();
    System.out.println("Inicio consulta \n");
    System.out.println(cadenaAConsultar);//it shows your query
    System.out.println("fin consulta \n");
    consulta.executeCommand(cadenaAConsultar);
}
于 2015-05-05T23:50:51.673 回答
0

据我所知,了解 eXist-db 的 Java 方面的人最好通过eXist-db 邮件列表exist-open联系到,而不是在 stackoverflow 上。我建议加入exist-open 并在那里发布您的问题。你不会失望的。此外,请记住包含您正在运行的 eXist 的版本号。祝你好运!

于 2012-04-19T21:43:59.843 回答